224 lines
5.4 KiB
TypeScript
224 lines
5.4 KiB
TypeScript
import {
|
|
CacheType,
|
|
Client,
|
|
GatewayIntentBits,
|
|
Interaction,
|
|
Message,
|
|
OAuth2Scopes,
|
|
PermissionFlagsBits,
|
|
VoiceState
|
|
} from 'discord.js';
|
|
import { Logger } from './utils/log';
|
|
import { config } from './utils/config';
|
|
import { Command, CommandCategory, CommandManager } from './commands';
|
|
import { DatabaseManager } from './modules/db';
|
|
|
|
type BotEventListeners = {
|
|
messageCreate: (message: Message) => void;
|
|
interactionCreate: (interaction: Interaction) => void;
|
|
voiceStateUpdate: (oldState: VoiceState, newState: VoiceState) => void;
|
|
};
|
|
type BotEventListener<K extends keyof BotEventListeners> = BotEventListeners[K];
|
|
|
|
export class Bot {
|
|
private client: Client | undefined;
|
|
private cmdMgr: CommandManager;
|
|
|
|
private readonly log: Logger;
|
|
|
|
private eventListeners: {
|
|
[K in keyof BotEventListeners]?: BotEventListener<K>[];
|
|
} = {};
|
|
|
|
/*
|
|
class methods
|
|
*/
|
|
private constructor() {
|
|
this.log = new Logger('Bot');
|
|
this.cmdMgr = new CommandManager('./commands');
|
|
}
|
|
|
|
public async init(): Promise<void> {
|
|
this.log.info('Bot init');
|
|
|
|
if (this.client)
|
|
throw new Error('Client already exists, was init called twice?');
|
|
|
|
this.log.info('Loading commands');
|
|
await this.cmdMgr.init();
|
|
|
|
this.log.info('Configuring database');
|
|
DatabaseManager.get.init(
|
|
this.cmdMgr.getUserKeys(),
|
|
this.cmdMgr.getGuildKeys()
|
|
);
|
|
|
|
this.log.info('Instantiating client');
|
|
this.client = new Client({
|
|
intents: [
|
|
GatewayIntentBits.Guilds,
|
|
GatewayIntentBits.GuildMessages,
|
|
GatewayIntentBits.GuildVoiceStates,
|
|
GatewayIntentBits.MessageContent
|
|
]
|
|
});
|
|
|
|
this.log.info('Registering callbacks');
|
|
this.client.on('clientReady', () => this.onReady());
|
|
this.client.on('error', (err) => this.onError(err, false));
|
|
this.client.on('shardError', (err) => this.onError(err, true));
|
|
this.client.on('messageCreate', (message: Message<boolean>) =>
|
|
this.onMessage(message)
|
|
);
|
|
this.client.on('interactionCreate', (interaction: Interaction<CacheType>) =>
|
|
this.onInteraction(interaction)
|
|
);
|
|
this.client.on(
|
|
'voiceStateUpdate',
|
|
(oldState: VoiceState, newState: VoiceState) =>
|
|
this.onVoiceStateUpdate(oldState, newState)
|
|
);
|
|
|
|
this.log.info('Logging in...');
|
|
await this.client.login(config.token);
|
|
}
|
|
|
|
public getCommands(): Array<Command> {
|
|
return this.cmdMgr.getAll();
|
|
}
|
|
|
|
public getCategories(): Array<CommandCategory> {
|
|
return this.cmdMgr.getCategories();
|
|
}
|
|
|
|
/*
|
|
event listeners
|
|
*/
|
|
private onReady(): void {
|
|
this.log.info('Logged in');
|
|
|
|
const inviteLink = this.client?.generateInvite({
|
|
scopes: [OAuth2Scopes.ApplicationsCommands, OAuth2Scopes.Bot],
|
|
permissions: [
|
|
PermissionFlagsBits.AddReactions,
|
|
PermissionFlagsBits.AttachFiles,
|
|
PermissionFlagsBits.ChangeNickname,
|
|
PermissionFlagsBits.Connect,
|
|
PermissionFlagsBits.Speak,
|
|
PermissionFlagsBits.ViewChannel,
|
|
PermissionFlagsBits.ReadMessageHistory,
|
|
PermissionFlagsBits.SendMessages,
|
|
PermissionFlagsBits.SendMessagesInThreads,
|
|
PermissionFlagsBits.SendVoiceMessages,
|
|
PermissionFlagsBits.EmbedLinks
|
|
]
|
|
});
|
|
|
|
this.log.info('Invite link: %s', inviteLink);
|
|
}
|
|
|
|
private onError(error: Error, isShardError: boolean): void {
|
|
if (isShardError)
|
|
this.log.error(
|
|
'A shard error ocurred: %s - %s - %s',
|
|
error.name,
|
|
error.message,
|
|
error.stack
|
|
);
|
|
else
|
|
this.log.error(
|
|
'An error ocurred: %s - %s - %s',
|
|
error.name,
|
|
error.message,
|
|
error.stack
|
|
);
|
|
}
|
|
|
|
/*
|
|
public event listeners
|
|
*/
|
|
private async onInteraction(interaction: Interaction): Promise<void> {
|
|
this.emit('interactionCreate', interaction);
|
|
}
|
|
|
|
private async onMessage(message: Message): Promise<void> {
|
|
this.emit('messageCreate', message);
|
|
}
|
|
|
|
private async onVoiceStateUpdate(
|
|
oldState: VoiceState,
|
|
newState: VoiceState
|
|
): Promise<void> {
|
|
this.emit('voiceStateUpdate', oldState, newState);
|
|
}
|
|
|
|
/*
|
|
registerable event system
|
|
*/
|
|
public on<K extends keyof BotEventListeners>(
|
|
event: K,
|
|
listener: BotEventListener<K>
|
|
): void {
|
|
if (!this.eventListeners[event]) {
|
|
this.eventListeners[event] = [];
|
|
}
|
|
(this.eventListeners[event] as BotEventListener<K>[]).push(listener);
|
|
}
|
|
|
|
public off<K extends keyof BotEventListeners>(
|
|
event: K,
|
|
listener: BotEventListener<K>
|
|
): boolean {
|
|
const listeners = this.eventListeners[event];
|
|
if (!listeners) return false;
|
|
|
|
const index = (listeners as BotEventListener<K>[]).indexOf(listener);
|
|
if (index > -1) {
|
|
(listeners as BotEventListener<K>[]).splice(index, 1);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public once<K extends keyof BotEventListeners>(
|
|
event: K,
|
|
listener: BotEventListener<K>
|
|
): void {
|
|
const onceWrapper = ((...args: Parameters<BotEventListener<K>>) => {
|
|
this.off(event, onceWrapper as BotEventListener<K>);
|
|
(listener as (...args: unknown[]) => void)(...args);
|
|
}) as BotEventListener<K>;
|
|
|
|
this.on(event, onceWrapper);
|
|
}
|
|
|
|
private emit<K extends keyof BotEventListeners>(
|
|
event: K,
|
|
...args: Parameters<BotEventListener<K>>
|
|
): void {
|
|
const listeners = this.eventListeners[event];
|
|
if (listeners) {
|
|
for (const listener of listeners as BotEventListener<K>[]) {
|
|
try {
|
|
(listener as (...args: unknown[]) => void)(...args);
|
|
} catch (error) {
|
|
this.log.error(
|
|
`Error in event listener for ${String(event)}:`,
|
|
error
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
singleton logic
|
|
*/
|
|
static #instance: Bot | null = null;
|
|
|
|
public static get get(): Bot {
|
|
if (!Bot.#instance) Bot.#instance = new Bot();
|
|
return Bot.#instance;
|
|
}
|
|
}
|