generated from nhcarrigan/template
343d712e33
Coordinates respondToMention and notifyNameMention so only one DM is sent per message, regardless of how many keyword patterns are triggered.
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
/**
|
|
* @copyright NHCarrigan
|
|
* @license Naomi's Public License
|
|
* @author Naomi Carrigan
|
|
*/
|
|
|
|
import { MessageFlags, type Message } from "discord.js";
|
|
import { ids } from "../config/ids.js";
|
|
import { getComponentsForNaomi } from "../utils/getComponentsForNaomi.js";
|
|
import { logger } from "../utils/logger.js";
|
|
import type { Amari } from "../interfaces/amari.js";
|
|
|
|
/**
|
|
* Checks if a message mentions Naomi or our team role.
|
|
* If so, responds.
|
|
* @param amari -- Amari's instance.
|
|
* @param message -- The guild message payload from Discord.
|
|
* @returns Whether a DM notification was sent.
|
|
*/
|
|
// eslint-disable-next-line complexity -- Mainly those reply options...
|
|
export const respondToMention = async(
|
|
amari: Amari,
|
|
message: Message<true>,
|
|
): Promise<boolean> => {
|
|
try {
|
|
const naomi = amari.discord.users.cache.get(ids.users.naomi)
|
|
?? await amari.discord.users.fetch(ids.users.naomi);
|
|
const { mentions, content, author, url, channel } = message;
|
|
if (author.bot || author.id === ids.users.naomi) {
|
|
return false;
|
|
}
|
|
if (amari.recentlyActiveChannels.has(channel.id)) {
|
|
return false;
|
|
}
|
|
const mentionsNaomi = mentions.has(ids.users.naomi, {
|
|
ignoreEveryone: true,
|
|
ignoreRepliedUser: true,
|
|
ignoreRoles: true,
|
|
}) || /naomi/i.test(content);
|
|
const mentionsNHCarrigan = mentions.has(ids.roles.nhcarrigan, {
|
|
ignoreEveryone: true,
|
|
ignoreRepliedUser: true,
|
|
}) || mentions.has(ids.users.nhcarrigan, {
|
|
ignoreEveryone: true,
|
|
ignoreRepliedUser: true,
|
|
ignoreRoles: true,
|
|
}) || /nhcarrigan/i.test(content);
|
|
if (!mentionsNaomi && !mentionsNHCarrigan) {
|
|
return false;
|
|
}
|
|
await naomi.send(
|
|
{
|
|
components: getComponentsForNaomi(author, content, url),
|
|
flags: [ MessageFlags.IsComponentsV2 ],
|
|
},
|
|
);
|
|
await logger.metric("processed_mention", 1, { pingType: mentionsNaomi
|
|
? "naomi"
|
|
: "nhcarrigan", user: author.id });
|
|
return true;
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
await logger.error("respond to mention module", error);
|
|
}
|
|
return false;
|
|
}
|
|
};
|