discord-bot-rss-feed/wrapper/index.js
benji7425 5f64fac0a5 git subrepo clone git@github.com:benji7425/shell-discord-bot.git wrapper
subrepo:
  subdir:   "wrapper"
  merged:   "4946884"
upstream:
  origin:   "git@github.com:benji7425/shell-discord-bot.git"
  branch:   "master"
  commit:   "4946884"
git-subrepo:
  version:  "0.3.1"
  origin:   "???"
  commit:   "???"
2017-04-22 22:41:44 +01:00

67 lines
2.1 KiB
JavaScript

//node imports
const Console = require("console");
//external module imports
var Discord = require("discord.io");
var BotModules = [];
var bot;
var EventHandlers = {
onReady: () => {
Console.info("Registered bot " + bot.username + " with id " + bot.id);
for (let i = 0, len = BotModules.length; i < len; i++) {
let botModule = BotModules[i];
if (botModule.onReady) botModule.onReady();
}
},
onDisconnect: (err, code) => {
Console.error("Bot was disconnected!", err, code);
for (let i = 0, len = BotModules.length; i < len; i++) {
let botModule = BotModules[i];
if (botModule.onReady) botModule.onDisconnect();
}
bot.connect();
},
onMessage: (user, userID, channelID, message) => {
for (let i = 0, iLen = BotModules.length; i < iLen; i++) {
let botModule = BotModules[i];
if (botModule.commands) {
for (let j = 0, jLen = botModule.commands.length; j < jLen; j++) {
let messageTrigger = botModule.commands[j];
if ((!messageTrigger.channelIDs && !messageTrigger.userIDs) //if we have neither channel nor user restraint, pass
|| (messageTrigger.channelIDs && messageTrigger.channelIDs.includes(channelID)) //otherwise, if we have a channel constraint, pass if we're allowed to respond in this channel
|| (messageTrigger.userIDs && messageTrigger.userIDs.includes(userID))) //otherwise, if we have a user constraint, pass if we're allowed to respond to this user
switch (messageTrigger.type) {
case "startsWith":
if (message.startsWith(messageTrigger.command))
messageTrigger.action(bot, user, userID, channelID, message);
break;
default:
if (message === messageTrigger.command)
messageTrigger.action(bot, user, userID, channelID, message);
}
}
}
if (botModule.onMessage) botModule.onMessage(bot, user, userID, channelID, message);
}
}
};
(() => {
bot = new Discord.Client({
token: require("./token.json").token,
autorun: true
});
bot.on("ready", EventHandlers.onReady);
bot.on("disconnect", EventHandlers.onDisconnect);
bot.on("message", EventHandlers.onMessage);
})();