123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- import * as dotenv from "dotenv";
- dotenv.config();
- import * as fs from "fs";
- import * as path from "path";
- import { client } from "./client";
- import { ICommand, BotEvent, IBotCommand } from "./commands/command"
- import "reflect-metadata";
- import {createConnection} from "typeorm";
- import { migrate } from "./lowdb_migrator";
- import { documentation } from "./util";
- const REACT_PROBABILITY = 0.3;
- async function trigger(actions : BotEvent[], ...params: any[]) {
- let actionDone = false;
- for (let i = 0; i < actions.length; i++) {
- const action = actions[i];
- let actionResult = action(actionDone, ...params);
- if(actionResult instanceof Promise)
- actionDone = (await actionResult) || actionDone;
- else
- actionDone = actionResult || actionDone;
- }
- return actionDone;
- }
- let commands : IBotCommand[] = [];
- let msgActions : BotEvent[] = [];
- let indirectMentionActions : BotEvent[] = [];
- let startActions : Array<() => void | Promise<void>> = [];
- let directMessageActions : BotEvent[] = [];
- let postActions : BotEvent[] = [];
- client.on("ready", async () => {
- 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];
- let val = action();
- if(val instanceof Promise)
- await val;
- }
- console.log("NoctBot is ready!");
- });
- client.on("message", async m => {
- if (m.author.id == client.user.id)
- return;
- let content = m.cleanContent.trim();
- if (await trigger(msgActions, m, content))
- return;
- if (m.mentions.users.size > 0 && m.mentions.users.has(client.user.id)) {
- if (m.content.trim().startsWith(client.user.toString())) {
- 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 (await trigger(directMessageActions, m, lowerCaseContent))
- return;
- }
- if (await trigger(indirectMentionActions, m))
- return;
- }
- await 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);
- }
- });
- async function main() {
- await createConnection();
- await migrate();
- let commandsPath = path.resolve(path.dirname(module.filename), "commands");
- let files = fs.readdirSync(commandsPath);
- for (const file of files) {
- let ext = path.extname(file);
- let name = path.basename(file);
-
- if(name == "command.js")
- continue;
- if (ext != ".js")
- continue;
- let obj = require(path.resolve(commandsPath, file)).default as ICommand;
- if (obj.commands)
- for (let command of obj.commands) {
- commands.push(command);
- }
- if (obj.documentation)
- for (let command in obj.documentation) {
- if (obj.documentation.hasOwnProperty(command))
- 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();
|