From 767f7d7da402d2e45d32b7aa1b74bf7028e51b93 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Thu, 30 May 2024 12:38:46 +0800 Subject: [PATCH] Improve bootstrap function Allow env variables to be substitutable. --- packages/discord-bot/src/index.ts | 29 +++++++++++++++++++++-------- packages/discord-bot/src/log.ts | 5 +++++ 2 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 packages/discord-bot/src/log.ts diff --git a/packages/discord-bot/src/index.ts b/packages/discord-bot/src/index.ts index eb0a2c4..298c966 100644 --- a/packages/discord-bot/src/index.ts +++ b/packages/discord-bot/src/index.ts @@ -4,10 +4,17 @@ import {addMessageEventsListener} from './events'; import {ForumChannel} from 'discord.js'; import {setupThreads} from './modules/thread'; import {BotContext} from './common'; +import * as logger from './log'; -const main = async () => { +interface Params { + logger?: Pick; + config: typeof config; +} + +const main = async (params = {} as Params) => { + const { logger: l = logger, config: c, } = params const client = createClient({ - intents: config.meta.intentsInteger, + intents: c.meta.intentsInteger, }); const context: BotContext = { @@ -23,25 +30,31 @@ const main = async () => { client.on('ready', async () => { if (!client.user) { - console.error('Not logged in!'); + l.error('Not logged in!'); return; } - console.log(`Logged in as ${client.user.tag}`); + l.log(`Logged in as ${client.user.tag}!`); - const channel = await client.channels.fetch(config.meta.channelId); + const channel = await client.channels.fetch(c.meta.channelId); if (!(channel instanceof ForumChannel)) { - console.error(`Channel ${config.meta.channelId} is not a forum!`); + l.error(`Channel ${c.meta.channelId} is not a forum!`); return; } context.channel = channel; + l.log('Syncing up threads...'); await setupThreads(context); + l.log('Sync done! Now listening for new messages.'); }); addMessageEventsListener(context); - await client.login(config.meta.token); + l.log('Logging in to gateway...'); + await client.login(c.meta.token); } // TODO load channelId from config // TODO add discord wizard for chatting with bot for setup (e.g. selecting forum channel ID) -void main(); +void main({ + logger, + config, +}); diff --git a/packages/discord-bot/src/log.ts b/packages/discord-bot/src/log.ts new file mode 100644 index 0000000..d341777 --- /dev/null +++ b/packages/discord-bot/src/log.ts @@ -0,0 +1,5 @@ +export const log = (...args: unknown[]) => console.log(...args); + +export const error = (...args: unknown[]) => console.error(...args); + +export const warn = (...args: unknown[]) => console.warn(...args);