From 65adff92d070443686503fc33d06758db7eb2715 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sun, 8 Jan 2023 10:01:54 +0200 Subject: [PATCH] rolling back to refine later --- commands/general/joke.js | 60 ++++++++++++++++++------------ commands/general/urban.js | 65 ++++++++++++++++++--------------- commands/untils/8ball.js | 73 +++++++++++++++++++++---------------- commands/untils/activity.js | 35 ++++++++---------- commands/untils/qrgen.js | 51 ++++++++++++-------------- events/interactionCreate.js | 63 +++++++++++++++++++++++--------- handler/index.js | 62 +++++++++---------------------- 7 files changed, 215 insertions(+), 194 deletions(-) diff --git a/commands/general/joke.js b/commands/general/joke.js index ce6969a..b6c683b 100644 --- a/commands/general/joke.js +++ b/commands/general/joke.js @@ -1,31 +1,43 @@ -const { EmbedBuilder } = require('discord.js'); -const giveMeAJoke = require('give-me-a-joke'); +const { + EmbedBuilder +} = require('discord.js'); +var giveMeAJoke = require('give-me-a-joke'); module.exports = { name: "joke", description: "Gets a funny joke", - async run(client, interaction) { - let joke = await giveMeAJoke.getRandomDadJoke(); + run: async (client, interaction) => { + giveMeAJoke.getRandomDadJoke(function (joke) { - if (joke.includes("?")){ - let jokeData = joke.split("?"); - joke = `${jokeData[0]}?||${jokeData[1]}||`; - } - - const embed = createJokeEmbed(joke, interaction.user); - interaction.editReply({ embeds: [embed] }); + if (joke.includes("?")){ + let jokeData = joke.split("?") + const embed = new EmbedBuilder() + .setColor("#FF0000") + .setTitle("Here is your joke...") + .setDescription(jokeData[0] + "?||" + jokeData[1] + "||") + .setTimestamp() + .setFooter({ + text: `Requested by ${interaction.user.tag}`, + iconURL: `${interaction.user.displayAvatarURL()}` + }); + interaction.editReply({ + embeds: [embed] + }); + } else { + const embed = new EmbedBuilder() + .setColor("#FF0000") + .setTitle("Here is your joke...") + .setDescription(joke) + .setTimestamp() + .setFooter({ + text: `Requested by ${interaction.user.tag}`, + iconURL: `${interaction.user.displayAvatarURL()}` + }); + interaction.editReply({ + embeds: [embed] + }); + } + }) }, -}; - -function createJokeEmbed(joke, user) { - return new EmbedBuilder() - .setColor("#FF0000") - .setTitle("Here is your joke...") - .setDescription(joke) - .setTimestamp() - .setFooter({ - text: `Requested by ${user.tag}`, - iconURL: user.displayAvatarURL() - }); -} +}; \ No newline at end of file diff --git a/commands/general/urban.js b/commands/general/urban.js index edc5905..b5755c7 100644 --- a/commands/general/urban.js +++ b/commands/general/urban.js @@ -1,40 +1,47 @@ -const { EmbedBuilder } = require('discord.js'); -const urban = require('urban'); +const { + EmbedBuilder +} = require('discord.js'); +var urban = require('urban') +const isNotDefined = require("is-not-defined"); module.exports = { name: "urban", description: "Searches Urban Dictionary", options: [{ - name: "query", - description: "The thing you want to search for", - required: true, - type: 3 // 6 is type USER + "name": "query", + "description": "The thing you want to search for", + "required": true, + "type": 3 // 6 is type USER }], - async run(client, interaction) { - let searchWord = interaction.options._hoistedOptions[0].value; - let search = urban(searchWord); + run: async (client, interaction) => { - let data = await search.first(); + let searchWord = interaction.options._hoistedOptions[0].value + search = urban(searchWord); - if (data) { - const embed = createUrbanEmbed(data, searchWord, interaction.user); - interaction.editReply({ embeds: [embed] }); - } else { - interaction.editReply("Sorry, no results were found!"); - } - }, -}; + search.first(function(data) { -function createUrbanEmbed(data, searchWord, user) { - return new EmbedBuilder() - .setColor("#FF0000") - .setTitle(`Results for: ${searchWord}`) - .setDescription(`Definition: ${data.definition}`) - .addFields({ name: 'Example', value: data.example }) - .setTimestamp() - .setFooter({ - text: `Requested by ${user.tag}`, - iconURL: user.displayAvatarURL() + if (!isNotDefined(data)) { + + const embed = new EmbedBuilder() + .setColor("#FF0000") + .setTitle("Results for: " + searchWord) + .setDescription("Definition: " + data.definition) + .addFields({ + name: 'Example', + value: data.example + }) + .setTimestamp() + .setFooter({ + text: `Requested by ${interaction.user.tag}`, + iconURL: `${interaction.user.displayAvatarURL()}` + }); + interaction.editReply({ + embeds: [embed] + }); + } else { + return interaction.editReply("Sorry, no results were found!") + } }); -} + }, +}; \ No newline at end of file diff --git a/commands/untils/8ball.js b/commands/untils/8ball.js index a625708..cf8bf5d 100644 --- a/commands/untils/8ball.js +++ b/commands/untils/8ball.js @@ -4,40 +4,49 @@ module.exports = { name: "8ball", description: "Ask a question, get a response.", options: [{ - name: "question", - description: "The question you would like to ask.", - required: true, - type: 3 // 6 is type USER - }], + "name": "question", + "description": "The question you would like to ask.", + "required": true, + "type": 3 // 6 is type USER +}], run: async (client, interaction) => { - let question = interaction.options._hoistedOptions[0].value; + let question = interaction.options._hoistedOptions[0].value - const responses = [ - "It is certain", - "It is decidedly so", - "Reply hazy try again", - "Cannot predict now", - "Do not count on it", - "My sources say no", - "Outlook not so good", - "Signs point to yes" - ]; - - const response = responses[Math.floor(Math.random() * responses.length)]; - - const embed = create8BallEmbed(question, response, interaction.user); + function send(question, message){ + const embed = new EmbedBuilder() + .setColor("#FF0000") + .setTitle(question) + .setDescription(message) + .setTimestamp() + .setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` }); interaction.editReply({ embeds: [embed] }); + } + + var answer = Math.floor(Math.random() * 8); + + if (answer === 0) { + send(question, "it is certain") + } + else if (answer === 1) { + send(question, "It is decidedly so"); + } + else if (answer === 2) { + send(question, "Reply hazy try again"); + } + else if (answer === 3) { + send(question, "Cannot predict now"); + } + else if (answer === 4) { + send(question, "Do not count on it"); + } + else if (answer === 5) { + send(question, "My sources say no"); + } + else if (answer === 6) { + send(question, "Outlook not so good"); + } + else if (answer === 7) { + send(question, "Signs point to yes"); + } }, }; - -function create8BallEmbed(question, response, user) { - return new EmbedBuilder() - .setColor("#FF0000") - .setTitle(question) - .setDescription(response) - .setTimestamp() - .setFooter({ - text: `Requested by ${user.tag}`, - iconURL: user.displayAvatarURL() - }); -} diff --git a/commands/untils/activity.js b/commands/untils/activity.js index 1deada5..c02f115 100644 --- a/commands/untils/activity.js +++ b/commands/untils/activity.js @@ -1,31 +1,26 @@ const { EmbedBuilder } = require('discord.js'); -const unirest = require('unirest'); +var unirest = require('unirest'); module.exports = { name: "bored", description: "Find an activity to do when you are bored.", - async run(client, interaction) { - let response = await unirest + run: async (client, interaction) => { + + unirest .get('https://www.boredapi.com/api/activity') .headers({ 'Accept': 'application/json', 'Content-Type': 'application/json', - }); - - let data = response.body; - const embed = createBoredEmbed(data, interaction.user); - interaction.followUp({ embeds: [embed] }); + }) + .then((response) => { + let data = response.body + const embed = new EmbedBuilder() + .setColor("#FF0000") + .setTitle("Here is something to do!") + .setDescription(data.activity) + .setTimestamp() + .setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` }); + interaction.followUp({ embeds: [embed] }); + }) }, }; - -function createBoredEmbed(data, user) { - return new EmbedBuilder() - .setColor("#FF0000") - .setTitle("Here is something to do!") - .setDescription(data.activity) - .setTimestamp() - .setFooter({ - text: `Requested by ${user.tag}`, - iconURL: user.displayAvatarURL() - }); -} diff --git a/commands/untils/qrgen.js b/commands/untils/qrgen.js index 3cfacc6..f7e5a64 100644 --- a/commands/untils/qrgen.js +++ b/commands/untils/qrgen.js @@ -1,8 +1,7 @@ -const { MessageEmbed, Attachment, TextChannel } = require('discord.js'); +const { EmbedBuilder } = require('discord.js'); +const { AttachmentBuilder } = require('discord.js'); const { AwesomeQR } = require("awesome-qr"); -const fs = require("fs").promises; - -let fileName; +const fs = require("fs"); module.exports = { name: "qr", @@ -15,36 +14,32 @@ module.exports = { }], run: async (client, interaction) => { - if (!fileName) { - fileName = Math.floor(Math.random() * 99999).toString(); - } - const text = interaction.options._hoistedOptions[0].value; - const background = Buffer.from(fs.readFileSync("Terminal-icon.png")); - const buffer = await new AwesomeQR({ - text: text, - size: 500, - backgroundImage: background, - }).draw(); + (async () => { + let rand = Math.floor(Math.random() * 99999).toString(); + let text = interaction.options._hoistedOptions[0].value - await fs.writeFile(`./images/${fileName}.png`, buffer); - const file = new Attachment(`./images/${fileName}.png`); + const background = fs.readFileSync("Terminal-icon.png"); - const embed = new MessageEmbed() + const buffer = await new AwesomeQR({ + text: text, + size: 500, + backgroundImage: background, + + }).draw(); + + fs.writeFileSync("./images/" + rand + ".png", buffer); + const file = new AttachmentBuilder('./images/' + rand + ".png"); + + const embed = new EmbedBuilder() .setColor("#FF0000") .setTitle("Your Generated QR Code") .setTimestamp() - .setImage(`attachment://images/${fileName}.png`) + .setImage('attachment://images/' + rand + ".png") + .setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` }); - try { - const message = await interaction.channel.send({ embeds: [embed], files: [file] }); - setTimeout(async () => { - // await message.delete(); - console.log(message) - await fs.unlink(`./images/${fileName}.png`); - }, 10000); - } catch (error) { - console.error(error); - } + interaction.editReply({ embeds: [embed], files: [file] }); + })(); + }, }; diff --git a/events/interactionCreate.js b/events/interactionCreate.js index b6ce0de..7d05921 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -1,35 +1,63 @@ const client = require("../index"); require("dotenv").config(); -const fs = require("fs").promises; +const { glob } = require("glob"); +const { promisify } = require("util"); +const globPromise = promisify(glob); client.on("interactionCreate", async (interaction) => { + // Slash Commands - const slashCommands = await fs.readdir(`${process.cwd()}/commands/*/*.js`); - const arrayOfSlashCommands = slashCommands.map((filename) => { - const file = require(`${process.cwd()}/commands/${filename}`); + const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`); + const arrayOfSlashCommands = []; + 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; - return file; + arrayOfSlashCommands.push(file); }); // Slash Command Handling if (interaction.isChatInputCommand()) { - const commandData = arrayOfSlashCommands.find( - (command) => command.name === interaction.commandName - ); - if (!commandData) return; - const cmd = client.slashCommands.get(interaction.commandName); - if (!cmd) return interaction.followUp({ content: "An error has occurred " }); - - const args = interaction.options.data.map((option) => { - if (option.type === "SUB_COMMAND") { - if (option.name) return option.name; - return option.options?.map((x) => x.value).filter((x) => x); + let commandData = [] + await arrayOfSlashCommands.forEach(command => { + console.log(command.name) + if (command.name == interaction.commandName) { + commandData.push(command) } - return option.value; }); + let dataToProcess = JSON.stringify(commandData[0]) + let parsedData = JSON.parse(dataToProcess) + + 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); @@ -41,4 +69,5 @@ client.on("interactionCreate", async (interaction) => { const command = client.slashCommands.get(interaction.commandName); if (command) command.run(client, interaction); } + }); diff --git a/handler/index.js b/handler/index.js index 70d55af..6598979 100644 --- a/handler/index.js +++ b/handler/index.js @@ -1,60 +1,34 @@ require("dotenv").config(); -const fs = require("fs").promises; +const { glob } = require("glob"); +const { promisify } = require("util"); +const globPromise = promisify(glob); module.exports = async (client) => { // Slash Commands - const slashCommands = await fs.readdir(`${process.cwd()}/commands/*/*.js`); - const arrayOfSlashCommands = slashCommands.map((filename) => { - const file = require(`${process.cwd()}/commands/${filename}`); + const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`); + const arrayOfSlashCommands = []; + 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; - return file; + arrayOfSlashCommands.push(file); }); // Events - const eventFiles = await fs.readdir(`${process.cwd()}/events/*.js`); - eventFiles.forEach((filename) => require(`${process.cwd()}/events/${filename}`)); + const eventFiles = await globPromise(`${process.cwd()}/events/*.js`); + eventFiles.map((value) => require(value)); // Slash Commands Register client.on("ready", async () => { // Register for all the guilds the bot is in await client.application.commands.set(arrayOfSlashCommands); - }); - client.on("interactionCreate", async (interaction) => { - // Slash Command Handling - if (interaction.isChatInputCommand()) { - let commandData = arrayOfSlashCommands.find((command) => command.name === interaction.commandName); - if (!commandData) return interaction.followUp({ content: "An error has occurred " }); - 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); - - if (commandData.private) { - await interaction.deferReply({ ephemeral: true }).catch(() => { }); - } else { - await interaction.deferReply({ ephemeral: false }).catch(() => { }); - } - - 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); - } - }); -} \ No newline at end of file +};