Skip to content

Commit 726556c

Browse files
mattmattvanvoorstmatt-mirovcarl
authored
reatibot-report-vc-bot-messages-to-separate-channel (#365)
* reatibot-report-vc-bot-messages-to-separate-channel Rework logger into a Map, so we can identify loggers better. Our logger now logs into a specific channel (and the default logger). This allows us to more granularly log into specific channels. VC specific message are now logged into a separate channel, to declutter the bot-log * reatibot-report-vc-bot-messages-to-separate-channel Variable name change * Update src/constants/channels.ts Co-authored-by: Carl Vitullo <[email protected]> * reatibot-report-vc-bot-messages-to-separate-channel I feel a lot prettier --------- Co-authored-by: matt-miro <[email protected]> Co-authored-by: Carl Vitullo <[email protected]>
1 parent eac2360 commit 726556c

File tree

7 files changed

+42
-18
lines changed

7 files changed

+42
-18
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ dist
66
yarn.lock
77
*.log
88
k8s-context
9-
pnpm-lock.yaml
9+
pnpm-lock.yaml
10+
.idea

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
1. `npm run dev`
2929
1. Look for the following message in the logs, and open the URL in a browser where you're logged into Discord.
3030
- `Bot started. If necessary, add it to your test server:`
31+
- Make sure to not install this bot directly on Reactiflux but on the Reactiflux Test Server. Ask for the correct role in RF's #reactibot channel
3132

3233
# Implementation notes
3334

package-lock.json

Lines changed: 4 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/constants/channels.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const LOCAL_CHANNELS: Record<keyof typeof PRODUCTION_CHANNELS, string> = {
1818
techReadsAndNews: "950790520811184150",
1919
modLog: "925847644318879754",
2020
botLog: "916081991542276096",
21+
vcLog: "1237103703740121201",
2122
};
2223

2324
const PRODUCTION_CHANNELS = {
@@ -37,6 +38,7 @@ const PRODUCTION_CHANNELS = {
3738
techReadsAndNews: "105816607976095744",
3839
modLog: "591326408396111907",
3940
botLog: "701462381703856158",
41+
vcLog: "1237161125473161347",
4042
};
4143

4244
export const CHANNELS = isProd() ? PRODUCTION_CHANNELS : LOCAL_CHANNELS;

src/features/log.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ChannelType, Client } from "discord.js";
2+
import { CHANNELS } from "../constants/channels";
23

34
type Logger = (type: string, text: string) => void;
45

@@ -27,10 +28,30 @@ export const channelLog =
2728
}
2829
};
2930

30-
const loggers: Logger[] = [stdoutLog];
31+
type LoggerKey = keyof typeof CHANNELS | "stdout";
32+
type LoggerObj = { id: LoggerKey; logger: Logger };
33+
type LoggerMap = Map<LoggerKey | "stdout", Logger>;
34+
35+
const loggers: LoggerMap = new Map([["stdout", stdoutLog]]);
3136

3237
export const logger = {
33-
add: (logger: Logger) => loggers.push(logger),
34-
log: (type: string, text: string) =>
35-
loggers.map((logger) => logger(type, text)),
38+
add: ({ id, logger }: LoggerObj) => loggers.set(id, logger),
39+
remove: (loggerId: LoggerObj["id"]) => loggers.delete(loggerId),
40+
log: (type: string, text: string, logName: LoggerKey = "botLog") => {
41+
const defaultLogger = loggers.get("stdout");
42+
const logger = loggers.get(logName);
43+
44+
if (!defaultLogger) {
45+
console.error(`Default logger not found`);
46+
return;
47+
}
48+
49+
if (!logger) {
50+
console.error(`Logger with id ${logName} not found`);
51+
return;
52+
}
53+
54+
defaultLogger(type, text);
55+
logger(type, text);
56+
},
3657
};

src/features/voice-activity.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import {
44
VoiceBasedChannel,
55
VoiceChannel,
66
} from "discord.js";
7-
import { logger } from "./log";
7+
import { channelLog, logger } from "./log";
88
import { scheduleTask } from "../helpers/schedule";
9+
import { CHANNELS } from "../constants/channels";
910

1011
const voiceChannelJoinTimestamps: Record<string, Record<string, number>> = {};
1112

@@ -22,6 +23,8 @@ const getTimeInChannel = (
2223
};
2324

2425
const voiceActivity = (bot: Client) => {
26+
logger.add({ id: "vcLog", logger: channelLog(bot, CHANNELS.vcLog) });
27+
2528
scheduleTask("voice activity", 3 * 60 * 1000, () => {
2629
// Fetch all voice channels
2730
const voiceChannels = bot.channels.cache.filter(
@@ -44,6 +47,7 @@ const voiceActivity = (bot: Client) => {
4447
logger.log(
4548
"VOICE",
4649
`${members.length} in <#${channel.id}>:\n${members.join("\n")}`,
50+
"vcLog",
4751
);
4852
}
4953
});
@@ -59,7 +63,7 @@ const voiceActivity = (bot: Client) => {
5963
voiceChannelJoinTimestamps[channel.id] ??= {};
6064
voiceChannelJoinTimestamps[channel.id][member.id] = Date.now();
6165

62-
logger.log("VOICE", `<@${member.id}> joined <#${channel.id}>.`);
66+
logger.log("VOICE", `<@${member.id}> joined <#${channel.id}>.`, "vcLog");
6367
} else if (!channel) {
6468
const { channel: cachedChannel } = oldState;
6569
if (!cachedChannel) return;
@@ -72,6 +76,7 @@ const voiceActivity = (bot: Client) => {
7276
cachedChannel,
7377
member.id,
7478
)} minutes.`,
79+
"vcLog",
7580
);
7681
}
7782
});

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ const handleReaction = (
163163
};
164164

165165
initCachedChannels(bot);
166-
logger.add(channelLog(bot, CHANNELS.botLog));
166+
logger.add({ id: "botLog", logger: channelLog(bot, CHANNELS.botLog) });
167167

168168
// Amplitude metrics
169169
setupStats(bot);

0 commit comments

Comments
 (0)