generated from nhcarrigan/template
feat: deduplicate mentee welcome messages
Track welcomed mentees in data/welcomed.txt to prevent sending the onboarding message multiple times if the mentorship role is re-assigned.
This commit is contained in:
@@ -6,6 +6,10 @@
|
||||
|
||||
import { ids } from "../config/ids.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import {
|
||||
addWelcomedMentee,
|
||||
welcomedMentees,
|
||||
} from "../utils/welcomedMentees.js";
|
||||
import type { Amari } from "../interfaces/amari.js";
|
||||
import type { GuildMember, PartialGuildMember } from "discord.js";
|
||||
|
||||
@@ -25,6 +29,7 @@ export const processMentorshipRole = async(
|
||||
if (
|
||||
oldMember.roles.cache.has(ids.roles.mentorship)
|
||||
|| !updatedMember.roles.cache.has(ids.roles.mentorship)
|
||||
|| welcomedMentees.has(updatedMember.id)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
@@ -52,6 +57,7 @@ Last name:
|
||||
\`\`\`
|
||||
Then read our [mentorship wiki](<https://docs.nhcarrigan.com/mentorship/00-faq/>) for the next steps!`,
|
||||
});
|
||||
addWelcomedMentee(updatedMember.id);
|
||||
await logger.metric("processed_mentorship_role", 1, {
|
||||
user: updatedMember.id,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { appendFileSync, existsSync, readFileSync } from "node:fs";
|
||||
|
||||
const filePath = "data/welcomed.txt";
|
||||
|
||||
const loadWelcomedMentees = (): Set<string> => {
|
||||
if (!existsSync(filePath)) {
|
||||
return new Set();
|
||||
}
|
||||
const contents = readFileSync(filePath, "utf-8");
|
||||
return new Set(contents.split("\n").filter(Boolean));
|
||||
};
|
||||
|
||||
const welcomedMentees = loadWelcomedMentees();
|
||||
|
||||
/**
|
||||
* Appends a mentee's ID to the welcomed set and persists it to disk.
|
||||
* @param id - The Discord user ID to record.
|
||||
*/
|
||||
const addWelcomedMentee = (id: string): void => {
|
||||
welcomedMentees.add(id);
|
||||
appendFileSync(filePath, `${id}\n`);
|
||||
};
|
||||
|
||||
export { addWelcomedMentee, welcomedMentees };
|
||||
Reference in New Issue
Block a user