Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/constants/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,6 +20,7 @@ type ChannelTypeMap = {
showcaseDigest: TextChannel;
spamDetection: TextChannel;
archiveCategory: CategoryChannel;
botLogs: TextChannel;
};

const EXPECTED_DISCORD_TYPE: Record<ChannelKey, ChannelType> = {
Expand All @@ -31,6 +33,7 @@ const EXPECTED_DISCORD_TYPE: Record<ChannelKey, ChannelType> = {
showcaseDigest: ChannelType.GuildText,
spamDetection: ChannelType.GuildText,
archiveCategory: ChannelType.GuildCategory,
botLogs: ChannelType.GuildText,
};

const resolveChannel = <Key extends ChannelKey>(
Expand Down Expand Up @@ -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);
};
1 change: 1 addition & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
17 changes: 16 additions & 1 deletion src/util/logger.test.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -9,6 +9,7 @@ const buildPrefixRegex = (feature: string, level: string) =>

void describe('createLogger', () => {
afterEach(() => {
setLogChannel(undefined);
mock.restoreAll();
});

Expand Down Expand Up @@ -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$/
);
});
});
18 changes: 16 additions & 2 deletions src/util/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,32 @@ type Logger = {
[K in keyof typeof logMethod]: (...messages: unknown[]) => void;
};

type LogChannel = {
send: (message: string) => Promise<unknown>;
};

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.
*/
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;
};
Loading