generated from nhcarrigan/template
fix: properly handle duplicated channels
This commit is contained in:
@@ -3,15 +3,9 @@
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
import type { ProgressReminder } from "../interfaces/reminder.js";
|
||||
|
||||
interface Channel {
|
||||
channelId: string;
|
||||
roleId: string;
|
||||
name: string;
|
||||
createThread: boolean;
|
||||
}
|
||||
|
||||
const nhcarriganMentorshipChannels: Array<Channel> = [
|
||||
const nhcarriganMentorshipChannels: Array<ProgressReminder> = [
|
||||
{
|
||||
channelId: "1400630073010163792",
|
||||
createThread: true,
|
||||
@@ -19,7 +13,7 @@ const nhcarriganMentorshipChannels: Array<Channel> = [
|
||||
roleId: "1400588705273745550",
|
||||
},
|
||||
];
|
||||
const freeCodeCampSprintChannels: Array<Channel> = [
|
||||
const freeCodeCampSprintChannels: Array<ProgressReminder> = [
|
||||
{
|
||||
channelId: "1400935953887006861",
|
||||
createThread: true,
|
||||
@@ -49,7 +43,7 @@ const freeCodeCampSprintChannels: Array<Channel> = [
|
||||
/**
|
||||
* The channels to post progress reminders in.
|
||||
*/
|
||||
export const progressReminders: Array<Channel> = [
|
||||
export const progressReminders: Array<ProgressReminder> = [
|
||||
...nhcarriganMentorshipChannels,
|
||||
...freeCodeCampSprintChannels,
|
||||
];
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
export interface ProgressReminder {
|
||||
channelId: string;
|
||||
roleId: string;
|
||||
name: string;
|
||||
createThread: boolean;
|
||||
}
|
||||
@@ -3,70 +3,79 @@
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
import { ChannelType } from "discord.js";
|
||||
import { ChannelType, type TextChannel } from "discord.js";
|
||||
import { progressReminders } from "../config/progressReminders.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import type { Amari } from "../interfaces/amari.js";
|
||||
import type { ProgressReminder } from "../interfaces/reminder.js";
|
||||
|
||||
/**
|
||||
* Posts a daily progress check-in reminder in configured channels.
|
||||
* @param amari - Amari's instance.
|
||||
*/
|
||||
// eslint-disable-next-line max-lines-per-function -- shut up
|
||||
export const postProgressReminders = async(
|
||||
amari: Amari,
|
||||
): Promise<void> => {
|
||||
export const postProgressReminders = async(amari: Amari): Promise<void> => {
|
||||
try {
|
||||
const mapped = await Promise.all(
|
||||
progressReminders.map(async(channel) => {
|
||||
progressReminders.map(async(reminder) => {
|
||||
const fetched = await amari.discord.channels.
|
||||
fetch(channel.channelId).
|
||||
fetch(reminder.channelId).
|
||||
catch(() => {
|
||||
void logger.log("warn", `Failed to fetch channel ${channel.name}.`);
|
||||
void logger.log(
|
||||
"warn",
|
||||
`Failed to fetch channel ${reminder.name}.`,
|
||||
);
|
||||
return null;
|
||||
});
|
||||
if (!fetched) {
|
||||
await logger.log("warn", `Channel ${channel.name} not found.`);
|
||||
return null;
|
||||
await logger.log("warn", `Channel ${reminder.name} not found.`);
|
||||
return { channel: null, reminder: reminder };
|
||||
}
|
||||
if (fetched.type !== ChannelType.GuildText) {
|
||||
await logger.log(
|
||||
"warn",
|
||||
`Channel ${channel.name} is not a text channel.`,
|
||||
`Channel ${reminder.name} is not a text channel.`,
|
||||
);
|
||||
return null;
|
||||
return { channel: null, reminder: reminder };
|
||||
}
|
||||
return fetched;
|
||||
return { channel: fetched, reminder: reminder };
|
||||
}),
|
||||
);
|
||||
const filtered = mapped.filter((channel) => {
|
||||
const filtered: Array<{
|
||||
channel: TextChannel;
|
||||
reminder: ProgressReminder;
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Filter is dumb.
|
||||
}> = mapped.filter(({ channel }) => {
|
||||
return channel !== null;
|
||||
});
|
||||
}) as Array<{
|
||||
channel: TextChannel;
|
||||
reminder: ProgressReminder;
|
||||
}>;
|
||||
await Promise.all(
|
||||
filtered.map(async(channel) => {
|
||||
const isThread = progressReminders.find((c) => {
|
||||
return c.channelId === channel.id;
|
||||
filtered.map(async(reminder) => {
|
||||
const isThread
|
||||
= progressReminders.find((c) => {
|
||||
return c.channelId === reminder.channel.id;
|
||||
})?.createThread ?? false;
|
||||
const sent = await channel.
|
||||
send({ allowedMentions: {
|
||||
const sent = await reminder.channel.
|
||||
send({
|
||||
allowedMentions: {
|
||||
parse: [ "roles" ],
|
||||
},
|
||||
content:
|
||||
`Good morning <@&${
|
||||
progressReminders.find((c) => {
|
||||
return c.channelId === channel.id;
|
||||
})?.roleId ?? channel.guildId
|
||||
}> It is time for your daily progress update. Please share the following in ${isThread
|
||||
content: `Good morning <@&${reminder.reminder.roleId}> It is time for your daily progress update. Please share the following in ${
|
||||
isThread
|
||||
? "the thread attached to this message"
|
||||
: "this channel"}:
|
||||
: "this channel"
|
||||
}:
|
||||
|
||||
1️⃣ What did you accomplish yesterday?
|
||||
2️⃣ What are you working on today?
|
||||
3️⃣ Are there any blockers or issues you need help with?` }).
|
||||
3️⃣ Are there any blockers or issues you need help with?`,
|
||||
}).
|
||||
catch(() => {
|
||||
void logger.log(
|
||||
"warn",
|
||||
`Failed to send progress reminder in channel ${channel.name}.`,
|
||||
`Failed to send progress reminder in channel ${reminder.channel.name}.`,
|
||||
);
|
||||
return null;
|
||||
});
|
||||
@@ -79,7 +88,7 @@ export const postProgressReminders = async(
|
||||
catch(() => {
|
||||
void logger.log(
|
||||
"warn",
|
||||
`Failed to start thread in channel ${channel.name}.`,
|
||||
`Failed to start thread in channel ${reminder.channel.name}.`,
|
||||
);
|
||||
return null;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user