diff --git a/src/contextcommands/contextdel.ts b/src/contextcommands/contextdel.ts index 1d3c729..12dfee6 100644 --- a/src/contextcommands/contextdel.ts +++ b/src/contextcommands/contextdel.ts @@ -23,8 +23,9 @@ import { ContextMenuCommandType } from "discord.js"; import Logger from "../utils/Logger"; +import { dt } from "../main"; +import { deleteConfession } from "../commandutils"; import { messageOpts } from "../constants"; -import { BotClient } from "../bot"; const logger = new Logger("(:) contextdel"); @@ -37,12 +38,20 @@ export const data = new ContextMenuCommandBuilder() .setType(ApplicationCommandType.Message as ContextMenuCommandType); export async function execute(interaction: ContextMenuCommandInteraction) { - const text = `Target: ${interaction.targetId}`; + const { guildId, targetId } = interaction; - logger.log(text); + if (!dt.getConfessionById(guildId!, targetId)) { + return interaction.reply({ + content: "Either that confession wasn't found or you aren't allowed to remove it.", + ...messageOpts + }); + } - return interaction.reply({ - content: text, - ...messageOpts - }); + const { id: confessionId } = dt.getConfessionById(guildId!, targetId)!; + + try { + deleteConfession(interaction, confessionId); + } catch (err) { + logger.error("An error occured:", err); + } } diff --git a/src/storeman/client.ts b/src/storeman/client.ts index 68171e5..9f65133 100644 --- a/src/storeman/client.ts +++ b/src/storeman/client.ts @@ -182,6 +182,20 @@ export class StoreMan { return null; } + public getConfessionById(guildId: string, messageId: string): Confession | null { + for (const guild of this.data) { + if (guild.id === guildId) { + for (const confession of guild.confessions) { + if (confession.messageId === messageId) { + return confession; + } + } + } + } + + return null; + } + // Attempts to delete a confession. If it is sucessfully deleted, returns true, else false. public delConfesssion( { guild, user }: CommandInteraction,