Browse Source

Improve bootstrap function

Allow env variables to be substitutable.
master
TheoryOfNekomata 5 months ago
parent
commit
767f7d7da4
2 changed files with 26 additions and 8 deletions
  1. +21
    -8
      packages/discord-bot/src/index.ts
  2. +5
    -0
      packages/discord-bot/src/log.ts

+ 21
- 8
packages/discord-bot/src/index.ts View File

@@ -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<typeof console, 'log' | 'error' | 'warn'>;
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,
});

+ 5
- 0
packages/discord-bot/src/log.ts View File

@@ -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);

Loading…
Cancel
Save