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