Advertisement
M3rein

Untitled

Jul 27th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require('discord.js');
  2. const bot = new Discord.Client();
  3. var fs = require('fs');
  4. var servers = fs.readFileSync('servers.json');
  5. var config = JSON.parse(servers);
  6.  
  7. Array.prototype.contains = function(obj) {
  8.     var i = this.length;
  9.     while (i--) {
  10.         if (this[i] === obj) {
  11.             return true;
  12.         }
  13.     }
  14.     return false;
  15. }
  16.  
  17. function dateNow(){
  18.     var d = new Date();
  19.     var y = d.getFullYear();
  20.     var m = d.getMonth() + 1;
  21.     var dt = d.getDate();
  22.     var hr = d.getHours() + 1;
  23.     var mnts = d.getMinutes() + 1;
  24.     if (m < 10) { m = '0'+m.toString(); }
  25.     if (dt < 10) { dt = '0'+dt.toString(); }
  26.     if (hr < 10) { hr = '0'+hr.toString(); }
  27.     if (mnts < 10) { mnts = '0'+mnts.toString(); }
  28.     y = y.toString();
  29.     m = m.toString();
  30.     dt = dt.toString();
  31.     hr = hr.toString();
  32.     mnts = mnts.toString();
  33.     return y + '-' + m + '-' + dt + '__' + hr + ':' + mnts;
  34. }
  35.  
  36. function saveConfig(cfg){
  37.     var data = JSON.stringify(cfg, null, 2);
  38.     fs.writeFileSync('servers.json', data);
  39.     servers = fs.readFileSync('servers.json');
  40.     config = JSON.parse(servers);
  41.     console.log(config);
  42. }
  43.  
  44. function rand(int){
  45.     return Math.floor(Math.random() * parseInt(int));
  46. }
  47.  
  48. bot.on('ready', () => {
  49.     console.log('Vulpix online');
  50.     bot.user.setGame("Type v-config");
  51. });
  52.  
  53. bot.on('guildCreate', guild =>{
  54.     console.log('Vulpix joined "' + guild.name + '" server with ID "' + guild.id.toString() + '" at date: ' + Date.now() + '.');
  55.     guild.defaultChannel.sendMessage('Hello! I am Vulpix. I am here to help you out with utility commands, shortcuts, and more. Contact user `M3rein#7122` for questions and inquiries!');
  56.     var g = guild.id.toString(); // Default Config settings.
  57.     config[g] = {};
  58.     config[g]["prefix"] = "!";
  59.     config[g]["ignored_channels"] = [];
  60.     config[g]["disabled_commands"] = [];
  61.     config[g]["messages"] = {};
  62.     config[g]["messages"]["welcome"]["msg"] = "Welcome to the server, (user)!";
  63.     config[g]["messages"]["welcome"]["status"] = "on";
  64.     config[g]["messages"]["mute"]["msg"] = "(user) has been muted!"
  65.     config[g]["messages"]["mute"]["status"] = "on"
  66.     saveConfig(config);
  67. })
  68.  
  69. bot.on('guildMemberAdd', member =>{
  70.     if (config[member.guild.id.toString()]["messages"]["welcome"]["status"] == "on"){
  71.         var msg = config[member.guild.id.toString()]["messages"]["welcome"]["msg"];
  72.         msg = mes.replace('(user)', member.user.username);
  73.         member.guild.defaultChannel.sendMessage(msg);
  74.     }
  75. });
  76.  
  77. bot.on('message', message => {
  78.     var thisconfig = config[message.guild.id.toString()];
  79.     if (!thisconfig["ignored_channels"].contains(message.channel.name)){
  80.         if (message.content.startsWith(thisconfig["prefix"])){
  81.             cmd = message.content.split(thisconfig["prefix"])[1].split(' ')[0];
  82.             args = message.content.split(" ");
  83.             args.splice(0, 1);
  84.             console.log(dateNow() + ' ' + message.author.username + `: ` + message.content);
  85.             if (cmd == "choose"){
  86.                 _args = message.content.split(' ');
  87.                 var str = "";
  88.                 for (i = 1; i < _args.length; i++){
  89.                     str += _args[i];
  90.                     if (i != _args.length - 1) { str += " "; }
  91.                 }
  92.                 options = str.split('|');
  93.                 message.channel.sendMessage(options[rand(options.length)]);
  94.             }
  95.             else if (cmd == "show_database"){
  96.                 var dat = fs.readFileSync('servers.json');
  97.                 var cfg = JSON.parse(dat, null, 2);
  98.                 console.log(cfg);
  99.             }
  100.         }
  101.         else if (message.content.startsWith("v-config")){ // Configuration of the bot for the server.
  102.             args = message.content.split(" ");
  103.             args.splice(0, 1);
  104.             var param = args[0];
  105.             var setting = args[1];
  106.             console.log(dateNow() + ' ' + message.author.username + `: ` + message.content);
  107.             if (param == "prefix"){
  108.                 if (setting != undefined){
  109.                     config[message.guild.id.toString()]["prefix"] = setting;
  110.                     saveConfig(config);
  111.                     message.channel.sendMessage('Successfully set active command prefix to `'+config[message.guild.id.toString()]["prefix"]+'`.');
  112.                 }
  113.                 else{
  114.                     message.channel.sendMessage('The prefix for commands is currently `'+thisconfig["prefix"]+'`.');
  115.                 }
  116.             }
  117.             else if (param == "messages"){
  118.                 var arg = args[1];
  119.                 if (arg == "welcome"){
  120.                     message.channel.sendMessage('The message that is sent whenever a new user joins.```Message: '+thisconfig["messages"]["welcome"]["msg"]+'\nStatus: '+thisconfig["messages"]["welcome"]["status"]+'```');
  121.                 }
  122.                 else if (arg == "mute"){
  123.                     message.channel.sendMessage('When you mute someone via the bot, this is the message that will be displayed. ```Mute message: '+thisconfig["messages"]["mute"]["msg"]+'\r\nStatus: '+thisconfig["messages"]["mute"]["status"]+'```');
  124.                 }
  125.                 else{
  126.                     message.channel.sendMessage('These are messages the bot will send under specific circumstances. You can turn them on/off and change the message. Use one of the following commands for more information:```v-config messages welcome\nv-config messages mute```');
  127.                 }
  128.             }
  129.             else if (param == "autorole"){
  130.                 message.channel.sendMessage('When a new user joins, you can choose for the bot to give them a role.```Role given: '+thisconfig["autorole"]+'\r\nStatus: '+thisconfig["autorole"]+'```Use one of the following commands to change the settings:```v-config autorole on\nv-config autorole off\nv-config autorole set (rolename)```');
  131.             }
  132.             else if (param == "ignored_channels"){
  133.                 var msg = 'If a command other than v-config is executed in any of the following channels, it will be ignored:```\n';
  134.                 if (thisconfig["ignored_channels"].length == 0) { msg += "---"; }
  135.                 else{
  136.                     for (i = 0; i < thisconfig["ignored_channels"].length; i++){
  137.                         msg += thisconfig["ignored_channels"][i];
  138.                         if (i != thisconfig["ignored_channels"].length - 1) { msg += '\n'; }
  139.                     }
  140.                 }
  141.                 msg += '``` Add or remove a channel with one of the following commands:```v-config ignored_channels add (channelname)\nv-config ignored_channels remove (channelname)```Channelname is the actual name of the channel, not a hyperlink or id.';
  142.                 message.channel.sendMessage(msg);
  143.             }
  144.             else if (param == "show"){
  145.                 message.channel.sendMessage('```JavaScript\n'+JSON.stringify(thisconfig, null, 2)+'```');
  146.             }
  147.             else{
  148.                 message.channel.sendMessage('To configure the bot for this server, use one of the following commands: ```v-config prefix\nv-config messages\nv-config autorole\nv-config ignored_channels\nv-config show```')
  149.             }
  150.         }
  151.     }
  152. });
  153.  
  154. bot.login('MzM5NzM5ODU5NTQ5NjgzNzEy.DFoW6g.LFoDAVvsDq77XIrkt0lBygFqXOw');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement