require("dotenv").config(); const fs = require("fs"); const path = require("path"); const client = require("./client.js"); const util = require("./util.js"); const REACT_PROBABILITY = 0.3; function trigger(actions, ...params) { let actionDone = false; for (let i = 0; i < actions.length; i++) { const action = actions[i]; actionDone |= action(...params, actionDone); } return actionDone; } let commands = []; let msgActions = []; let indirectMentionActions = []; let startActions = []; let directMessageActions = []; let postActions = []; client.on("ready", () => { console.log("Starting up NoctBot!"); client.user.setActivity(process.env.NODE_ENV == "dev" ? "Maintenance" : "@NoctBot help", { type: "PLAYING" }); for (let i = 0; i < startActions.length; i++) { const action = startActions[i]; action(); } console.log("NoctBot is ready!"); }); client.on("message", m => { if (m.author.id == client.user.id) return; let content = m.cleanContent.trim(); if (!util.shouldShowMaintenanceMessage(m.guild.id) && trigger(msgActions, m, content)) return; if (m.mentions.users.size > 0 && m.mentions.users.first().id == client.user.id) { if(util.shouldShowMaintenanceMessage(m.guild.id)) { m.channel.send(`${m.author.toString()} I'm currently being maintained; please wait.`); return; } if (content.startsWith(`@${client.user.username}`)) { content = content.substring(`@${client.user.username}`.length).trim(); let lowerCaseContent = content.toLowerCase(); for (let c of commands) { if (typeof(c.pattern) == "string" && lowerCaseContent.startsWith(c.pattern)) { c.action(m, content); return; } else if(c.pattern instanceof RegExp){ let result = c.pattern.exec(content); if(result != null){ c.action(m, content, result); return; } } } if (trigger(directMessageActions, m, lowerCaseContent)) return; } if (trigger(indirectMentionActions, m)) return; } if(!util.shouldShowMaintenanceMessage(m.guild.id)) trigger(postActions); }); client.on("messageReactionAdd", (r, u) => { if (Math.random() <= REACT_PROBABILITY && !u.bot) { console.log(`Reacting to message ${r.message.id} because user ${u.tag} reacted to it`); r.message.react(r.emoji); } }); function main() { let commandsPath = path.resolve(path.dirname(module.filename), "commands"); let files = fs.readdirSync(commandsPath); for (let i = 0; i < files.length; i++) { const file = files[i]; let ext = path.extname(file); if (ext != ".js") continue; let obj = require(path.resolve(commandsPath, file)); if (obj.commands) { for (let command of obj.commands) { // if (obj.commands.hasOwnProperty(command)) // commands[command] = obj.commands[command]; commands.push(command); } } if (obj.documentation) { for (let command in obj.documentation) { if (obj.documentation.hasOwnProperty(command)) util.documentation[command] = obj.documentation[command]; } } if (obj.onMessage) msgActions.push(obj.onMessage); if (obj.onIndirectMention) indirectMentionActions.push(obj.onIndirectMention); if (obj.onDirectMention) directMessageActions.push(obj.onDirectMention); if (obj.postMessage) postActions.push(obj.postMessage); if (obj.onStart) startActions.push(obj.onStart); } client.login(process.env.TOKEN); } main();