mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-18 23:09:59 +00:00
74 lines
2.6 KiB
TypeScript
74 lines
2.6 KiB
TypeScript
import { GuildChannel, Permissions } from "discord.js";
|
|
import { LogType } from "../../../data/LogType";
|
|
import { isDiscordAPIError } from "../../../utils";
|
|
import { getMissingChannelPermissions } from "../../../utils/getMissingChannelPermissions";
|
|
import { missingPermissionError } from "../../../utils/missingPermissionError";
|
|
import { readChannelPermissions } from "../../../utils/readChannelPermissions";
|
|
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
|
import { autoReactionsEvt } from "../types";
|
|
import { AutoReaction } from "../../../data/entities/AutoReaction";
|
|
|
|
const p = Permissions.FLAGS;
|
|
|
|
export const AddReactionsEvt = autoReactionsEvt({
|
|
event: "messageCreate",
|
|
allowBots: true,
|
|
allowSelf: true,
|
|
|
|
async listener({ pluginData, args: { message } }) {
|
|
let autoReaction: AutoReaction | null = null;
|
|
const lock = await pluginData.locks.acquire(`auto-reactions-${message.channel.id}`);
|
|
if (pluginData.state.cache.has(message.channel.id)) {
|
|
autoReaction = pluginData.state.cache.get(message.channel.id) ?? null;
|
|
} else {
|
|
autoReaction = (await pluginData.state.autoReactions.getForChannel(message.channel.id)) ?? null;
|
|
pluginData.state.cache.set(message.channel.id, autoReaction);
|
|
}
|
|
lock.unlock();
|
|
|
|
if (!autoReaction) {
|
|
return;
|
|
}
|
|
|
|
const me = pluginData.guild.members.cache.get(pluginData.client.user!.id)!;
|
|
if (me) {
|
|
const missingPermissions = getMissingChannelPermissions(
|
|
me,
|
|
message.channel as GuildChannel,
|
|
readChannelPermissions | p.ADD_REACTIONS,
|
|
);
|
|
if (missingPermissions) {
|
|
const logs = pluginData.getPlugin(LogsPlugin);
|
|
logs.logBotAlert({
|
|
body: `Cannot apply auto-reactions in <#${message.channel.id}>. ${missingPermissionError(
|
|
missingPermissions,
|
|
)}`,
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
|
|
for (const reaction of autoReaction.reactions) {
|
|
try {
|
|
await message.react(reaction);
|
|
} catch (e) {
|
|
if (isDiscordAPIError(e)) {
|
|
const logs = pluginData.getPlugin(LogsPlugin);
|
|
if (e.code === 10008) {
|
|
logs.logBotAlert({
|
|
body: `Could not apply auto-reactions in <#${message.channel.id}> for message \`${message.id}\`. Make sure nothing is deleting the message before the reactions are applied.`,
|
|
});
|
|
} else {
|
|
logs.logBotAlert({
|
|
body: `Could not apply auto-reactions in <#${message.channel.id}> for message \`${message.id}\`. Error code ${e.code}.`,
|
|
});
|
|
}
|
|
|
|
break;
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
},
|
|
});
|