Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reatibot-report-vc-bot-messages-to-separate-channel #365

Merged
Show file tree
Hide file tree
Changes from 2 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
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
mattmattvanvoorst marked this conversation as resolved.
Show resolved Hide resolved

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: '' // TODO: add me once we have channel in prod
mattmattvanvoorst marked this conversation as resolved.
Show resolved Hide resolved
};

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
Loading