contrition/src/bot/Bot.ts

99 lines
2.8 KiB
TypeScript

import {
AkairoClient,
CommandHandler,
InhibitorHandler,
ListenerHandler,
} from "discord-akairo";
import { EventEmitter } from "events";
import { Config } from "../config/Config";
import { parseDuration } from "../config/Constants";
import { Appeal } from "../entity/Appeal";
import { errorEmbed } from "./Util";
class Bot extends AkairoClient {
static config: Config;
inhibitorHandler: InhibitorHandler;
commandHandler: CommandHandler;
listenerHandler: ListenerHandler;
constructor(config: Config, appealEmitter: EventEmitter) {
super({}, { partials: ["REACTION", "MESSAGE"] });
Bot.config = config;
super.login(config.bot.token);
// register inhibitors in inhibitors directory
this.inhibitorHandler = new InhibitorHandler(this, {
directory: `${__dirname}/inhibitors`,
});
// register commands in commands directory
this.commandHandler = new CommandHandler(this, {
directory: `${__dirname}/commands`,
prefix: config.bot.prefix,
// command defaults
argumentDefaults: {
prompt: {
retries: 0,
time: 15000,
cancel: () => errorEmbed("Command Cancelled"),
ended: () =>
errorEmbed("Invalid input").setDescription("Command ended."),
timeout: () =>
errorEmbed("Timed out").setDescription(
"No response received for 15 seconds."
),
},
},
});
// register listeners in listeners directory
this.listenerHandler = new ListenerHandler(this, {
directory: `${__dirname}/listeners`,
});
this.listenerHandler.setEmitters({
commandHandler: this.commandHandler,
inhibitorHandler: this.inhibitorHandler,
listenerHandler: this.listenerHandler,
appealHandler: appealEmitter,
});
this.commandHandler.useInhibitorHandler(this.inhibitorHandler);
this.commandHandler.resolver.addType("timestring", (_, phrase) => {
if (!phrase) return null;
const d = parseDuration(phrase);
if (d === "") return null;
return d;
});
this.commandHandler.resolver.addType("appeal", async (message, phrase) => {
if (!phrase) return null;
if (!message.guild) return null;
const appeal = await Appeal.appeal(phrase, message.guild.id)
.then((a) => a)
.catch((_) => null);
return appeal;
});
this.commandHandler.resolver.addType("optionalDate", (_, phrase) => {
if (!phrase) return null;
if (phrase === "never") return "never";
if (phrase === "now") return "now";
const timestamp = Date.parse(phrase);
if (isNaN(timestamp)) return null;
return new Date(timestamp);
});
this.inhibitorHandler.loadAll();
this.commandHandler.loadAll();
this.listenerHandler.loadAll();
}
}
export default Bot;