|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +// Load up libraries |
| 4 | +const Discord = require("discord.js"); |
| 5 | +// Load config! |
| 6 | +let config = require("config"); |
| 7 | +config = config.get("bot"); |
| 8 | + |
| 9 | +var aliases; |
| 10 | +try { |
| 11 | + aliases = require("./alias.json"); |
| 12 | +} catch (e) { |
| 13 | + //No aliases defined |
| 14 | + aliases = { |
| 15 | + test: { |
| 16 | + process: function(bot, msg) { |
| 17 | + msg.channel.send("test"); |
| 18 | + } |
| 19 | + } |
| 20 | + }; |
| 21 | +} |
| 22 | +var commands = {}; |
| 23 | + |
| 24 | +var bot = new Discord.Client(); |
| 25 | + |
| 26 | +bot.on("ready", function() { |
| 27 | + console.log( |
| 28 | + "Logged in! Serving in " + bot.guilds.array().length + " servers" |
| 29 | + ); |
| 30 | + require("./plugins.js").init(); |
| 31 | + console.log("type " + config.prefix + "help in Discord for a commands list."); |
| 32 | + bot.user.setActivity(config.prefix + "Intialized!"); |
| 33 | + var text = ["tiprvn", "tipdoge"]; |
| 34 | + var counter = 0; |
| 35 | + setInterval(change, 10000); |
| 36 | + |
| 37 | + function change() { |
| 38 | + bot.user.setActivity(config.prefix + text[counter]); |
| 39 | + counter++; |
| 40 | + if (counter >= text.length) { |
| 41 | + counter = 0; |
| 42 | + } |
| 43 | + } |
| 44 | +}); |
| 45 | + |
| 46 | +bot.on("disconnected", function() { |
| 47 | + console.log("Disconnected!"); |
| 48 | + process.exit(1); //exit node.js with an error |
| 49 | +}); |
| 50 | + |
| 51 | +function checkMessageForCommand(msg, isEdit) { |
| 52 | + //check if message is a command |
| 53 | + if (msg.author.id != bot.user.id && msg.content.startsWith(config.prefix)) { |
| 54 | + console.log( |
| 55 | + "treating " + msg.content + " from " + msg.author + " as command" |
| 56 | + ); |
| 57 | + var cmdTxt = msg.content.split(" ")[0].substring(config.prefix.length); |
| 58 | + var suffix = msg.content.substring( |
| 59 | + cmdTxt.length + config.prefix.length + 1 |
| 60 | + ); //add one for the ! and one for the space |
| 61 | + if (msg.isMentioned(bot.user)) { |
| 62 | + try { |
| 63 | + cmdTxt = msg.content.split(" ")[1]; |
| 64 | + suffix = msg.content.substring( |
| 65 | + bot.user.mention().length + cmdTxt.length + config.prefix.length + 1 |
| 66 | + ); |
| 67 | + } catch (e) { |
| 68 | + //no command |
| 69 | + msg.channel.send("Yes?"); |
| 70 | + return; |
| 71 | + } |
| 72 | + } |
| 73 | + let alias = aliases[cmdTxt]; |
| 74 | + if (alias) { |
| 75 | + var cmd = alias; |
| 76 | + } else { |
| 77 | + var cmd = commands[cmdTxt]; |
| 78 | + } |
| 79 | + if (cmd) { |
| 80 | + // Add permission check here later on ;) |
| 81 | + try { |
| 82 | + cmd.process(bot, msg, suffix, isEdit); |
| 83 | + } catch (e) { |
| 84 | + var msgTxt = "command " + cmdTxt + " failed :("; |
| 85 | + if (config.debug) { |
| 86 | + msgTxt += "\n" + e.stack; |
| 87 | + } |
| 88 | + msg.channel.send(msgTxt); |
| 89 | + } |
| 90 | + } |
| 91 | + } else { |
| 92 | + //message isn't a command or is from us |
| 93 | + //drop our own messages to prevent feedback loops |
| 94 | + if (msg.author == bot.user) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if (msg.author != bot.user && msg.isMentioned(bot.user)) { |
| 99 | + msg.channel.send("yes?"); //using a mention here can lead to looping |
| 100 | + } else { |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +bot.on("message", msg => checkMessageForCommand(msg, false)); |
| 106 | +/*bot.on("messageUpdate", (oldMessage, newMessage) => { |
| 107 | + checkMessageForCommand(newMessage, true); |
| 108 | +});*/ |
| 109 | + |
| 110 | +exports.addCommand = function(commandName, commandObject) { |
| 111 | + try { |
| 112 | + commands[commandName] = commandObject; |
| 113 | + } catch (err) { |
| 114 | + console.log(err); |
| 115 | + } |
| 116 | +}; |
| 117 | +exports.addCustomFunc = function(customFunc) { |
| 118 | + try { |
| 119 | + customFunc(bot); |
| 120 | + } catch (err) { |
| 121 | + console.log(err); |
| 122 | + } |
| 123 | +}; |
| 124 | +exports.commandCount = function() { |
| 125 | + return Object.keys(commands).length; |
| 126 | +}; |
| 127 | + |
| 128 | +bot.login(config.token); |
0 commit comments