import { ExtendedClient as Client } from "utils/Client" import colorLog from "utils/functions/colors" import { ContextMenu } from "interfaces" import { color } from "config/options.json" import { Events } from "discord.js" import path from "path" import fs from "fs" import logToChannel from "utils/functions/logtochannel" type FileType = "js" | "ts" const embedColor = Number(color.replace("#", "0x")) export default function loadContextMenuEvents(client: Client, ft: FileType) { const contextMenuPath = path.join( __dirname, "..", "..", "commands-contextmenu", ) const contextMenuFiles = fs .readdirSync(contextMenuPath) .filter(file => file.endsWith(ft)) for (const file of contextMenuFiles) { const filePath = path.join(contextMenuPath, file) const cmd: ContextMenu = require(filePath) if ("data" in cmd && "execute" in cmd && cmd.type === "contextmenu") { client.contextmenus.set(cmd.data.name, cmd) } else { console.log( colorLog( `[WARNING] The command at ${filePath} is missing a required "data", "execute" or "type" property.`, "red", ), ) } } //! context menu command handler client.on(Events.InteractionCreate, async interaction => { if (!interaction.isContextMenuCommand()) return const command = client.contextmenus.get(interaction.commandName) if (!command) { console.error( `No command matching ${interaction.commandName} was found.`, ) return } try { await command.execute(interaction) } catch (error) { if (process.env.NODE_ENV !== "dev") { await logToChannel("error", { embeds: [ { title: "Contextmenu error occured", description: "```" + error + "```", color: embedColor, footer: { icon_url: interaction.guild!.iconURL() || undefined, text: interaction.user.username + " | " + interaction.commandName, }, }, ], }) } console.error(error) if (!interaction.deferred) { await interaction.reply({ embeds: [ { description: "There was an error while executing this contextmenu command!", color: embedColor, }, ], ephemeral: true, }) } else { await interaction.editReply({ embeds: [ { description: "There was an error while executing this contextmenu command!", color: embedColor, }, ], }) } } }) }