3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-07-11 04:57:19 +00:00
zeppelin/backend/src/plugins/Automod/events/runAutomodOnThreadEvents.ts
2021-10-26 23:21:04 +01:00

54 lines
1.9 KiB
TypeScript

import { typedGuildEventListener } from "knub";
import { GuildSavedMessages } from "../../../data/GuildSavedMessages";
import { runAutomod } from "../functions/runAutomod";
import { AutomodContext, AutomodPluginType } from "../types";
export const RunAutomodOnThreadCreate = typedGuildEventListener<AutomodPluginType>()({
event: "threadCreate",
async listener({ pluginData, args: { thread } }) {
const user = thread.ownerId ? await pluginData.client.users.fetch(thread.ownerId).catch(() => void 0) : void 0;
const context: AutomodContext = {
timestamp: Date.now(),
threadChange: {
created: thread,
},
user,
};
// This is a hack to make this trigger compatible with the reply action
const sourceChannel = thread.parent ?? pluginData.client.channels.cache.find((c) => c.id === thread.parentId);
messageBlock: if (sourceChannel?.isText()) {
const sourceMessage = sourceChannel.messages.cache.find(
(m) => m.thread?.id === thread.id || m.reference?.channelId === thread.id,
);
if (!sourceMessage) break messageBlock;
const savedMessage = pluginData.state.savedMessages.msgToSavedMessage(sourceMessage);
savedMessage.channel_id = thread.id;
context.message = savedMessage;
}
pluginData.state.queue.add(() => {
runAutomod(pluginData, context);
});
},
});
export const RunAutomodOnThreadDelete = typedGuildEventListener<AutomodPluginType>()({
event: "threadDelete",
async listener({ pluginData, args: { thread } }) {
const user = thread.ownerId ? await pluginData.client.users.fetch(thread.ownerId).catch(() => void 0) : void 0;
const context: AutomodContext = {
timestamp: Date.now(),
threadChange: {
deleted: thread,
},
user,
};
pluginData.state.queue.add(() => {
runAutomod(pluginData, context);
});
},
});