50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { Client as DJSClient } from '@projectdysnomia/dysnomia';
|
|
import kv, { asyncKV } from '@star-kitten/util/kv.js';
|
|
import type { KVStore } from './kv-store.type.ts.ts';
|
|
import type { Cache } from './cache.type.ts';
|
|
import { registerCommands } from '../commands/handle-commands.ts';
|
|
import type { Client } from './client.ts';
|
|
|
|
export interface DiscordBotOptions {
|
|
token?: string;
|
|
intents?: number[];
|
|
commandKey?: string;
|
|
keyStore?: KVStore;
|
|
cache?: Cache;
|
|
onError?: (error: Error) => void;
|
|
onReady?: () => void;
|
|
}
|
|
|
|
export function startBot({
|
|
token = process.env.DISCORD_BOT_TOKEN || '',
|
|
intents = [],
|
|
commandKey = 'default',
|
|
keyStore = asyncKV,
|
|
cache = kv,
|
|
onError,
|
|
onReady,
|
|
}: DiscordBotOptions = {}): Client {
|
|
const client = new DJSClient(`Bot ${token}`, {
|
|
gateway: {
|
|
intents,
|
|
},
|
|
}) as Client;
|
|
client.commandKey = commandKey;
|
|
|
|
client.on('ready', async () => {
|
|
console.debug(`Logged in as ${client.user?.username}#${client.user?.discriminator}`);
|
|
onReady?.();
|
|
await registerCommands({ client, cache, kv: keyStore });
|
|
console.debug('Bot is ready and command handling is initialized.');
|
|
});
|
|
|
|
client.on('error', (error) => {
|
|
console.error('An error occurred:', error);
|
|
onError?.(error);
|
|
});
|
|
|
|
client.connect().catch(console.error);
|
|
|
|
return client;
|
|
}
|