From 6ded01b04688b1ca91a015571f4468b67e7b03cd Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Wed, 7 Feb 2024 21:02:37 +0100 Subject: [PATCH] handle message/channel deletion --- database.js | 5 ++++- events/channels/channelDelete.js | 9 +++++++++ events/messages/messageDelete.js | 9 +++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/database.js b/database.js index 0bcbc0e..c4e1222 100644 --- a/database.js +++ b/database.js @@ -11,10 +11,13 @@ const sequelize = new Sequelize({ dialect: 'sqlite', logging: false }); + const RoleEmojiPair = defineRoleEmojiPair(sequelize); + const VoiceChannel = defineVoiceChannel(sequelize); + const Message = defineMessage(sequelize); +Message.hasMany(RoleEmojiPair, { foreignKey: 'message', onDelete: 'CASCADE' }); sequelize.sync(); - export { sequelize, RoleEmojiPair, VoiceChannel, Message }; diff --git a/events/channels/channelDelete.js b/events/channels/channelDelete.js index ff32238..6564da4 100644 --- a/events/channels/channelDelete.js +++ b/events/channels/channelDelete.js @@ -1,5 +1,14 @@ import { Events } from 'discord.js'; +import { VoiceChannel } from '../../database.js'; export const name = Events.ChannelDelete; export async function execute(channel) { + // Delete channel entry once channel is deleted itself + const count = await VoiceChannel.destroy({ + where: { + id: channel.id + } + }); + if (count > 0) + console.info(`[INFO] Custom VC with ID '${channel.id}' was deleted.`); } diff --git a/events/messages/messageDelete.js b/events/messages/messageDelete.js index 9272a83..3975c95 100644 --- a/events/messages/messageDelete.js +++ b/events/messages/messageDelete.js @@ -1,5 +1,14 @@ import { Events } from 'discord.js'; +import { Message } from '../../database.js'; export const name = Events.MessageDelete; export async function execute(message) { + // Delete message entry once message is deleted itself + const count = await Message.destroy({ + where: { + id: message.id + } + }); + if (count > 0) + console.info(`[INFO] Reaction Roles Message with ID '${message.id}' was deleted.`); }