diff --git a/.gitignore b/.gitignore index 6b07ce58..fdb2c6bb 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ dist yarn.lock *.log k8s-context -pnpm-lock.yaml \ No newline at end of file +pnpm-lock.yaml +.idea \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3dcd492a..6db60d4e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 diff --git a/package-lock.json b/package-lock.json index 050b44d5..9fa191ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,7 @@ "version": "1.0.0", "license": "MIT", "dependencies": { - "@types/node-cron": "3.0.1", "date-fns": "2.27.0", - "discord-api-types": "0.33.4", "discord.js": "14.11.0", "dotenv": "10.0.0", "gists": "2.0.0", @@ -27,6 +25,7 @@ }, "devDependencies": { "@types/node": "20.6.0", + "@types/node-cron": "3.0.1", "@types/node-fetch": "2.5.4", "@types/open-graph-scraper": "4.8.1", "@typescript-eslint/eslint-plugin": "5.9.0", @@ -389,6 +388,7 @@ }, "node_modules/@types/node-cron": { "version": "3.0.1", + "dev": true, "license": "MIT" }, "node_modules/@types/node-fetch": { @@ -1294,10 +1294,6 @@ "node": ">=8" } }, - "node_modules/discord-api-types": { - "version": "0.33.4", - "license": "MIT" - }, "node_modules/discord.js": { "version": "14.11.0", "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-14.11.0.tgz", @@ -5084,7 +5080,8 @@ "integrity": "sha512-najjVq5KN2vsH2U/xyh2opaSEz6cZMR2SetLIlxlj08nOcmPOemJmUK2o4kUzfLqfrWE0PIrNeE16XhYDd3nqg==" }, "@types/node-cron": { - "version": "3.0.1" + "version": "3.0.1", + "dev": true }, "@types/node-fetch": { "version": "2.5.4", @@ -5660,9 +5657,6 @@ "path-type": "^4.0.0" } }, - "discord-api-types": { - "version": "0.33.4" - }, "discord.js": { "version": "14.11.0", "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-14.11.0.tgz", diff --git a/src/constants/channels.ts b/src/constants/channels.ts index 27078529..837a4d7e 100644 --- a/src/constants/channels.ts +++ b/src/constants/channels.ts @@ -18,6 +18,7 @@ const LOCAL_CHANNELS: Record = { techReadsAndNews: "950790520811184150", modLog: "925847644318879754", botLog: "916081991542276096", + vcLog: "1237103703740121201", }; const PRODUCTION_CHANNELS = { @@ -37,6 +38,7 @@ const PRODUCTION_CHANNELS = { techReadsAndNews: "105816607976095744", modLog: "591326408396111907", botLog: "701462381703856158", + vcLog: "1237161125473161347", }; export const CHANNELS = isProd() ? PRODUCTION_CHANNELS : LOCAL_CHANNELS; diff --git a/src/features/log.ts b/src/features/log.ts index 99f2b7e8..bc5e6a3b 100644 --- a/src/features/log.ts +++ b/src/features/log.ts @@ -1,4 +1,5 @@ import { ChannelType, Client } from "discord.js"; +import { CHANNELS } from "../constants/channels"; type Logger = (type: string, text: string) => void; @@ -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; + +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); + }, }; diff --git a/src/features/voice-activity.ts b/src/features/voice-activity.ts index 4bcebc89..b9ace9c1 100644 --- a/src/features/voice-activity.ts +++ b/src/features/voice-activity.ts @@ -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> = {}; @@ -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( @@ -44,6 +47,7 @@ const voiceActivity = (bot: Client) => { logger.log( "VOICE", `${members.length} in <#${channel.id}>:\n${members.join("\n")}`, + "vcLog", ); } }); @@ -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; @@ -72,6 +76,7 @@ const voiceActivity = (bot: Client) => { cachedChannel, member.id, )} minutes.`, + "vcLog", ); } }); diff --git a/src/index.ts b/src/index.ts index ce88f9bb..e5a75641 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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);