diff --git a/.env.example b/.env.example index f8e204a..13fab63 100644 --- a/.env.example +++ b/.env.example @@ -13,6 +13,7 @@ ONBOARDING_CHANNEL_ID=onboarding_channel_id_here SPAM_DETECTION_CHANNEL_ID=spam_detection_channel_id_here ARCHIVE_CATEGORY_ID=archive_category_id_here SHOWCASE_DIGEST_CHANNEL_ID=showcase_digest_channel_id_here +BOT_LOGS_CHANNEL_ID=bot_logs_channel_id_here # Role IDs (REQUIRED) MODERATORS_ROLE_IDS=role_id_1,role_id_2,role_id_3 diff --git a/.env.production b/.env.production index bf2c30f..d986cf0 100644 --- a/.env.production +++ b/.env.production @@ -15,6 +15,7 @@ SHOWCASE_RULES_CHANNEL_ID=1517948527098073158 SPAM_DETECTION_CHANNEL_ID=973895712314110032 ARCHIVE_CATEGORY_ID=837507969859977258 SHOWCASE_DIGEST_CHANNEL_ID=1561474075745059017 +BOT_LOGS_CHANNEL_ID=1576765207697686699 # Role IDs (from your dev server) REPEL_ROLE_ID=1002411741776461844 diff --git a/.env.test b/.env.test index 22fc81b..8bcc3fa 100644 --- a/.env.test +++ b/.env.test @@ -22,6 +22,7 @@ SHOWCASE_RULES_CHANNEL_ID=your-showcase-rules-channel-id SPAM_DETECTION_CHANNEL_ID=spam-detection-channel-id-here ARCHIVE_CATEGORY_ID=your-archived-category-id SHOWCASE_DIGEST_CHANNEL_ID=your-showcase-digest-channel-id +BOT_LOGS_CHANNEL_ID=your-bot-log-channel-id # Role IDs (from your dev server) REPEL_ROLE_ID=your-repel-role-id diff --git a/src/constants/channels.ts b/src/constants/channels.ts index 000489a..41b554a 100644 --- a/src/constants/channels.ts +++ b/src/constants/channels.ts @@ -6,6 +6,7 @@ import type { } from 'discord.js'; import { ChannelType } from 'discord.js'; import { config } from '@/env.js'; +import { setLogChannel } from '@/util/logger.js'; export type ChannelKey = keyof typeof config.channelIds; @@ -19,6 +20,7 @@ type ChannelTypeMap = { showcaseDigest: TextChannel; spamDetection: TextChannel; archiveCategory: CategoryChannel; + botLogs: TextChannel; }; const EXPECTED_DISCORD_TYPE: Record = { @@ -31,6 +33,7 @@ const EXPECTED_DISCORD_TYPE: Record = { showcaseDigest: ChannelType.GuildText, spamDetection: ChannelType.GuildText, archiveCategory: ChannelType.GuildCategory, + botLogs: ChannelType.GuildText, }; const resolveChannel = ( @@ -71,4 +74,5 @@ export const resolveChannels = (guild: Guild): void => { (Object.keys(config.channelIds) as ChannelKey[]).forEach((key) => { assignResolvedChannel(key, resolveChannel(guild, key)); }); + setLogChannel(SERVER_CHANNELS.botLogs); }; diff --git a/src/env.ts b/src/env.ts index 80de945..a723758 100644 --- a/src/env.ts +++ b/src/env.ts @@ -49,6 +49,7 @@ export const config = { showcaseDigest: requireEnv('SHOWCASE_DIGEST_CHANNEL_ID'), spamDetection: requireEnv('SPAM_DETECTION_CHANNEL_ID'), archiveCategory: requireEnv('ARCHIVE_CATEGORY_ID'), + botLogs: requireEnv('BOT_LOGS_CHANNEL_ID'), }, onboarding: { channelId: optionalEnv('ONBOARDING_CHANNEL_ID'), diff --git a/src/util/logger.test.ts b/src/util/logger.test.ts index 48b5ca9..0286c1f 100644 --- a/src/util/logger.test.ts +++ b/src/util/logger.test.ts @@ -1,6 +1,6 @@ import assert from 'node:assert/strict'; import { afterEach, describe, it, mock } from 'node:test'; -import { createLogger } from './logger.js'; +import { createLogger, setLogChannel } from './logger.js'; const buildPrefixRegex = (feature: string, level: string) => new RegExp( @@ -9,6 +9,7 @@ const buildPrefixRegex = (feature: string, level: string) => void describe('createLogger', () => { afterEach(() => { + setLogChannel(undefined); mock.restoreAll(); }); @@ -45,4 +46,18 @@ void describe('createLogger', () => { assert.match(prefix as string, buildPrefixRegex('tags', 'ERROR')); assert.deepEqual(messages, ['Error deleting tag:', error]); }); + + void it('forwards logs to the configured channel', () => { + mock.method(console, 'info', () => {}); + const send = mock.fn(async (_message: string) => {}); + setLogChannel({ send }); + + createLogger('guides').info('synced', 42); + + assert.equal(send.mock.calls.length, 1); + assert.match( + send.mock.calls[0]!.arguments[0], + /^\[.*\] \[guides\] \[INFO\] synced 42$/ + ); + }); }); diff --git a/src/util/logger.ts b/src/util/logger.ts index 48eea37..2b4837f 100644 --- a/src/util/logger.ts +++ b/src/util/logger.ts @@ -11,6 +11,16 @@ type Logger = { [K in keyof typeof logMethod]: (...messages: unknown[]) => void; }; +type LogChannel = { + send: (message: string) => Promise; +}; + +let logChannel: LogChannel | undefined; + +export const setLogChannel = (channel: LogChannel | undefined): void => { + logChannel = channel; +}; + /** * Every line is prefixed with `[feature]` so logs can be filtered by feature name. */ @@ -18,11 +28,15 @@ export const createLogger = (feature: string): Logger => { return Object.fromEntries( Object.entries(logMethod).map(([method, level]) => [ method, - (...messages: unknown[]) => + (...messages: unknown[]) => { console[method as keyof Logger]( buildPrefix(feature, level), ...messages - ), + ); + void logChannel?.send( + `${buildPrefix(feature, level)} ${messages.join(' ')}` + ); + }, ]) ) as Logger; };