Skip to content

Commit

Permalink
reatibot-report-vc-bot-messages-to-separate-channel (#365)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored May 7, 2024
1 parent eac2360 commit 726556c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ dist
yarn.lock
*.log
k8s-context
pnpm-lock.yaml
pnpm-lock.yaml
.idea
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
1. `npm run dev`
1. Look for the following message in the logs, and open the URL in a browser where you're logged into Discord.
- `Bot started. If necessary, add it to your test server:`
- 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

# Implementation notes

Expand Down
14 changes: 4 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/constants/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const LOCAL_CHANNELS: Record<keyof typeof PRODUCTION_CHANNELS, string> = {
techReadsAndNews: "950790520811184150",
modLog: "925847644318879754",
botLog: "916081991542276096",
vcLog: "1237103703740121201",
};

const PRODUCTION_CHANNELS = {
Expand All @@ -37,6 +38,7 @@ const PRODUCTION_CHANNELS = {
techReadsAndNews: "105816607976095744",
modLog: "591326408396111907",
botLog: "701462381703856158",
vcLog: "1237161125473161347",
};

export const CHANNELS = isProd() ? PRODUCTION_CHANNELS : LOCAL_CHANNELS;
Expand Down
29 changes: 25 additions & 4 deletions src/features/log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChannelType, Client } from "discord.js";
import { CHANNELS } from "../constants/channels";

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

Expand Down Expand Up @@ -27,10 +28,30 @@ export const channelLog =
}
};

const loggers: Logger[] = [stdoutLog];
type LoggerKey = keyof typeof CHANNELS | "stdout";
type LoggerObj = { id: LoggerKey; logger: Logger };
type LoggerMap = Map<LoggerKey | "stdout", Logger>;

const loggers: LoggerMap = new Map([["stdout", stdoutLog]]);

export const logger = {
add: (logger: Logger) => loggers.push(logger),
log: (type: string, text: string) =>
loggers.map((logger) => logger(type, text)),
add: ({ id, logger }: LoggerObj) => loggers.set(id, logger),
remove: (loggerId: LoggerObj["id"]) => loggers.delete(loggerId),
log: (type: string, text: string, logName: LoggerKey = "botLog") => {
const defaultLogger = loggers.get("stdout");
const logger = loggers.get(logName);

if (!defaultLogger) {
console.error(`Default logger not found`);
return;
}

if (!logger) {
console.error(`Logger with id ${logName} not found`);
return;
}

defaultLogger(type, text);
logger(type, text);
},
};
9 changes: 7 additions & 2 deletions src/features/voice-activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
VoiceBasedChannel,
VoiceChannel,
} from "discord.js";
import { logger } from "./log";
import { channelLog, logger } from "./log";
import { scheduleTask } from "../helpers/schedule";
import { CHANNELS } from "../constants/channels";

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

Expand All @@ -22,6 +23,8 @@ const getTimeInChannel = (
};

const voiceActivity = (bot: Client) => {
logger.add({ id: "vcLog", logger: channelLog(bot, CHANNELS.vcLog) });

scheduleTask("voice activity", 3 * 60 * 1000, () => {
// Fetch all voice channels
const voiceChannels = bot.channels.cache.filter(
Expand All @@ -44,6 +47,7 @@ const voiceActivity = (bot: Client) => {
logger.log(
"VOICE",
`${members.length} in <#${channel.id}>:\n${members.join("\n")}`,
"vcLog",
);
}
});
Expand All @@ -59,7 +63,7 @@ const voiceActivity = (bot: Client) => {
voiceChannelJoinTimestamps[channel.id] ??= {};
voiceChannelJoinTimestamps[channel.id][member.id] = Date.now();

logger.log("VOICE", `<@${member.id}> joined <#${channel.id}>.`);
logger.log("VOICE", `<@${member.id}> joined <#${channel.id}>.`, "vcLog");
} else if (!channel) {
const { channel: cachedChannel } = oldState;
if (!cachedChannel) return;
Expand All @@ -72,6 +76,7 @@ const voiceActivity = (bot: Client) => {
cachedChannel,
member.id,
)} minutes.`,
"vcLog",
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const handleReaction = (
};

initCachedChannels(bot);
logger.add(channelLog(bot, CHANNELS.botLog));
logger.add({ id: "botLog", logger: channelLog(bot, CHANNELS.botLog) });

// Amplitude metrics
setupStats(bot);
Expand Down

0 comments on commit 726556c

Please sign in to comment.