feat: add extra event listeners

This commit is contained in:
2026-01-10 09:19:27 -03:00
parent 33adc4b22a
commit 88305e5c8b
+96 -77
View File
@@ -1,105 +1,124 @@
import { import {
Client, BaseInteraction,
GatewayIntentBits, CacheType,
OAuth2Scopes, Client,
PermissionFlagsBits GatewayIntentBits,
Interaction,
Message,
OAuth2Scopes,
PermissionFlagsBits,
VoiceState
} from 'discord.js'; } from 'discord.js';
import { Logger } from './utils/log'; import { Logger } from './utils/log';
import { config } from './utils/config'; import { config } from './utils/config';
export class Bot { export class Bot {
private client: Client | undefined; private client: Client | undefined;
private readonly token: string; private readonly token: string;
private readonly clientId: string; private readonly clientId: string;
private readonly log: Logger; private readonly log: Logger;
/* /*
class methods class methods
*/ */
private constructor() { private constructor() {
this.log = new Logger('Bot'); this.log = new Logger('Bot');
this.token = config.token; this.token = config.token;
this.clientId = config.client_id; this.clientId = config.client_id;
} }
public async init(): Promise<void> { public async init(): Promise<void> {
this.log.info('Bot init'); this.log.info('Bot init');
if (this.client) if (this.client)
throw new Error('Client already exists, was init called twice?'); throw new Error('Client already exists, was init called twice?');
this.log.info('Instantiating client'); this.log.info('Instantiating client');
this.client = new Client({ this.client = new Client({
intents: [ intents: [
GatewayIntentBits.Guilds, GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.MessageContent GatewayIntentBits.MessageContent
] ]
}); });
this.log.info('Registering callbacks'); this.log.info('Registering callbacks');
this.client.on('clientReady', () => this.onReady()); this.client.on('clientReady', () => this.onReady());
this.client.on('error', (err) => this.onError(err, false)); this.client.on('error', (err) => this.onError(err, false));
this.client.on('shardError', (err) => this.onError(err, true)); 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...'); this.log.info('Logging in...');
await this.client.login(this.token); await this.client.login(this.token);
} }
/* /*
event listeners event listeners
*/ */
private onReady(): void { private onReady(): void {
this.log.info('Logged in'); this.log.info('Logged in');
const inviteLink = this.client?.generateInvite({ const inviteLink = this.client?.generateInvite({
scopes: [OAuth2Scopes.ApplicationsCommands, OAuth2Scopes.Bot], scopes: [OAuth2Scopes.ApplicationsCommands, OAuth2Scopes.Bot],
permissions: [ permissions: [
PermissionFlagsBits.AddReactions, PermissionFlagsBits.AddReactions,
PermissionFlagsBits.AttachFiles, PermissionFlagsBits.AttachFiles,
PermissionFlagsBits.ChangeNickname, PermissionFlagsBits.ChangeNickname,
PermissionFlagsBits.Connect, PermissionFlagsBits.Connect,
PermissionFlagsBits.Speak, PermissionFlagsBits.Speak,
PermissionFlagsBits.ViewChannel, PermissionFlagsBits.ViewChannel,
PermissionFlagsBits.ReadMessageHistory, PermissionFlagsBits.ReadMessageHistory,
PermissionFlagsBits.SendMessages, PermissionFlagsBits.SendMessages,
PermissionFlagsBits.SendMessagesInThreads, PermissionFlagsBits.SendMessagesInThreads,
PermissionFlagsBits.SendVoiceMessages, PermissionFlagsBits.SendVoiceMessages,
PermissionFlagsBits.EmbedLinks PermissionFlagsBits.EmbedLinks
] ]
}); });
this.log.info('Invite link: %s', inviteLink); this.log.info('Invite link: %s', inviteLink);
} }
private onError(error: Error, isShardError: boolean): void { private onError(error: Error, isShardError: boolean): void {
if (isShardError) if (isShardError)
this.log.error( this.log.error(
'A shard error ocurred: %s - %s - %s', 'A shard error ocurred: %s - %s - %s',
error.name, error.name,
error.message, error.message,
error.stack error.stack
); );
else else
this.log.error( this.log.error(
'An error ocurred: %s - %s - %s', 'An error ocurred: %s - %s - %s',
error.name, error.name,
error.message, error.message,
error.stack error.stack
); );
} }
private async onInteraction(interaction: BaseInteraction): Promise<void> {
/* }
private async onMessage(message: Message<boolean>): Promise<void> {
}
private async onVoiceStateUpdate(oldState: VoiceState, newState: VoiceState): Promise<void> {
}
/*
singleton logic singleton logic
*/ */
static #instance: Bot | null = null; static #instance: Bot | null = null;
public static get get(): Bot { public static get get(): Bot {
if (!Bot.#instance) Bot.#instance = new Bot(); if (!Bot.#instance) Bot.#instance = new Bot();
return Bot.#instance; return Bot.#instance;
} }
} }