From d88e07a7ed7aaafd3d51838e00a288b0b66a5f42 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Wed, 28 Sep 2022 18:00:36 -0400 Subject: [PATCH 1/3] adding a private option within the command config and adding ephemeral support by default --- .gitignore | 3 ++ commands/Info/ping.js | 1 + events/interactionCreate.js | 99 ++++++++++++++++++++++++++++++------- startBot.json | 9 ++++ 4 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 .gitignore create mode 100644 startBot.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5693cd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +.env +package-lock.json diff --git a/commands/Info/ping.js b/commands/Info/ping.js index 18ab6c7..1eac0a6 100644 --- a/commands/Info/ping.js +++ b/commands/Info/ping.js @@ -2,6 +2,7 @@ const { EmbedBuilder } = require('discord.js'); module.exports = { name: "ping", + private: false, description: "Returns websocket latency", run: async (client, interaction) => { diff --git a/events/interactionCreate.js b/events/interactionCreate.js index 27e56bf..19e5a41 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -1,33 +1,94 @@ const client = require("../index"); +require("dotenv").config(); +const { glob } = require("glob"); +const { promisify } = require("util"); +const globPromise = promisify(glob); client.on("interactionCreate", async (interaction) => { + + // Slash Commands + const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`); + const arrayOfSlashCommands = []; + + // Map the slash commands into data to be processed + slashCommands.map((value) => { + const file = require(value); + const splitted = value.split("/"); + const directory = splitted[splitted.length - 2]; + + if (!file?.name) return; + + const properties = { + directory, + ...file + }; + client.slashCommands.set(file.name, properties); + + if (["MESSAGE", "USER"].includes(file.type)) delete file.description; + + // Push the data + arrayOfSlashCommands.push(file); + }); + + // Slash Command Handling if (interaction.isChatInputCommand()) { - await interaction.deferReply({ ephemeral: false }).catch(() => { }); - const cmd = client.slashCommands.get(interaction.commandName); - if (!cmd) - return interaction.followUp({ content: "An error has occurred " }); + // Grabbing Command Data for this interaction + let commandData = [] - const args = []; + // We use ForEach here to filter our array into the single commands info. + await arrayOfSlashCommands.forEach(command => { + if (command.name == interaction.commandName) { + commandData.push(command) + } + }); - for (let option of interaction.options.data) { - if (option.type === "SUB_COMMAND") { - if (option.name) args.push(option.name); - option.options?.forEach((x) => { - if (x.value) args.push(x.value); - }); - } else if (option.value) args.push(option.value); - } - interaction.member = interaction.guild.members.cache.get(interaction.user.id); + // Process and Parse Data + let dataToProcess = JSON.stringify(commandData[0]) + let parsedData = JSON.parse(dataToProcess) - cmd.run(client, interaction, args); + // If the command is private, set ephemeral true else, set false + console.log(parsedData) + if (parsedData.private == true) { + await interaction.deferReply({ + ephemeral: true + }).catch(() => {}); + + } else { + await interaction.deferReply({ + ephemeral: false + }).catch(() => {}); + } + + + const cmd = client.slashCommands.get(interaction.commandName); + if (!cmd) + return interaction.followUp({ + content: "An error has occurred " + }); + + const args = []; + + for (let option of interaction.options.data) { + if (option.type === "SUB_COMMAND") { + if (option.name) args.push(option.name); + option.options?.forEach((x) => { + if (x.value) args.push(x.value); + }); + } else if (option.value) args.push(option.value); + } + interaction.member = interaction.guild.members.cache.get(interaction.user.id); + + cmd.run(client, interaction, args); } // Context Menu Handling if (interaction.isContextMenuCommand()) { - await interaction.deferReply({ ephemeral: false }); - const command = client.slashCommands.get(interaction.commandName); - if (command) command.run(client, interaction); + await interaction.deferReply({ + ephemeral: false + }); + const command = client.slashCommands.get(interaction.commandName); + if (command) command.run(client, interaction); } -}); +}); \ No newline at end of file diff --git a/startBot.json b/startBot.json new file mode 100644 index 0000000..bdf91de --- /dev/null +++ b/startBot.json @@ -0,0 +1,9 @@ +{ + "apps": [ + { + "name": "bot-copy", + "script": "node index.js", + "args" : "" + } + ] +} From cbc6740e465bbfbebf23deee5992edaad3848f71 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Wed, 28 Sep 2022 18:02:34 -0400 Subject: [PATCH 2/3] Removing files --- startBot.json | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 startBot.json diff --git a/startBot.json b/startBot.json deleted file mode 100644 index bdf91de..0000000 --- a/startBot.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "apps": [ - { - "name": "bot-copy", - "script": "node index.js", - "args" : "" - } - ] -} From 93c3dfa65e09350c80aa02490b177e734d467279 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Wed, 28 Sep 2022 19:05:07 -0400 Subject: [PATCH 3/3] Adding an Example ContentMenu Command --- commands/Info/ping.js | 2 +- commands/context/ping.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 commands/context/ping.js diff --git a/commands/Info/ping.js b/commands/Info/ping.js index 1eac0a6..9a6b3d6 100644 --- a/commands/Info/ping.js +++ b/commands/Info/ping.js @@ -2,7 +2,7 @@ const { EmbedBuilder } = require('discord.js'); module.exports = { name: "ping", - private: false, + private: true, description: "Returns websocket latency", run: async (client, interaction) => { diff --git a/commands/context/ping.js b/commands/context/ping.js new file mode 100644 index 0000000..ed946c5 --- /dev/null +++ b/commands/context/ping.js @@ -0,0 +1,16 @@ +const { EmbedBuilder } = require('discord.js'); +const {ApplicationCommandType } = require('discord.js'); + +module.exports = { + name: "ping-test", + type: ApplicationCommandType.Message, + run: async (client, interaction) => { + const embed = new EmbedBuilder() + .setColor("#FF0000") + .setTitle("🏓 Pong!") + .setDescription(`Latency : ${client.ws.ping}ms`) + .setTimestamp() + .setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` }); + interaction.followUp({ embeds: [embed] }); + }, +};