123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- const TOKEN = require("./token.js");
- const fs = require("fs");
- const path = require("path");
- const client = require("./client.js");
- const util = require("./util.js");
- const REACT_PROBABILITY = 0.6;
- 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("@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 (trigger(msgActions, m, content))
- return;
- if (m.mentions.users.size > 0 && m.mentions.users.first().id == client.user.id) {
- 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;
- }
- trigger(postActions);
- });
- client.on("messageReactionAdd", (r, u) => {
- if (Math.random() <= REACT_PROBABILITY && !r.me)
- 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(TOKEN);
- }
- main();
|