From 8d59ac37a6f7450d5e827415194d3466f892cf27 Mon Sep 17 00:00:00 2001 From: Supositware Date: Thu, 1 Sep 2022 01:43:59 +0200 Subject: [PATCH] New way to handle args --- commands/admin/bye.js | 10 ++++----- commands/admin/welcome.js | 12 +++++------ commands/fun/4chan.js | 2 +- commands/fun/fakeuser.js | 10 ++++----- commands/fun/reddit.js | 2 +- commands/fun/tweet.js | 4 ++-- commands/fun/ytp.js | 4 ++-- commands/secret/s.js | 2 +- commands/utility/download.js | 6 +++--- commands/utility/feedback.js | 2 +- commands/utility/help.js | 10 ++++++--- commands/utility/vid2gif.js | 2 +- events/client/interactionCreate.js | 14 ++++++------ events/client/messageCreate.js | 34 +++++++++++------------------- 14 files changed, 55 insertions(+), 59 deletions(-) diff --git a/commands/admin/bye.js b/commands/admin/bye.js index 553156b2..741d8463 100644 --- a/commands/admin/bye.js +++ b/commands/admin/bye.js @@ -13,11 +13,11 @@ export default { async execute(interaction, args, client) { const leave = await db.leaveChannel.findOne({ where: { guildID: interaction.guild.id } }); - if (!leave && !args[0]) { + if (!leave && !args.message) { return interaction.reply({ content: 'You need a message for me to say anything!', ephemeral: true }); } else if (!leave) { - const body = { guildID: interaction.guild.id, channelID: interaction.channel.id, message: args[0] }; + const body = { guildID: interaction.guild.id, channelID: interaction.channel.id, message: args.message }; await db.leaveChannel.create(body); return interaction.reply({ content: `The leave message have been set with ${args.message}`, ephemeral: true }); } @@ -48,12 +48,12 @@ export default { if (!interactionMenu.isButton) return; interactionMenu.update({ components: [] }); if (interactionMenu.customId === 'edit') { - if (!args[0]) { + if (!args.message) { return interaction.reply({ content: 'You need to input a message for me to edit!', ephemeral: true }); } - const body = { guildID: interaction.guild.id, channelID: interaction.channel.id, message: args[0] }; + const body = { guildID: interaction.guild.id, channelID: interaction.channel.id, message: args.message }; await db.leaveChannel.update(body, { where: { guildID: interaction.guild.id } }); - return interaction.editReply({ content: `The leave message has been set to ${args[0]}`, ephemeral: true }); + return interaction.editReply({ content: `The leave message has been set to ${args.message}`, ephemeral: true }); } else if (interactionMenu.customId === 'remove') { db.leaveChannel.destroy({ where: { guildID: interaction.guild.id, channelID: interaction.channel.id } }); diff --git a/commands/admin/welcome.js b/commands/admin/welcome.js index ac4044a6..5d70fd09 100644 --- a/commands/admin/welcome.js +++ b/commands/admin/welcome.js @@ -13,13 +13,13 @@ export default { async execute(interaction, args, client) { const join = await db.joinChannel.findOne({ where: { guildID: interaction.guild.id } }); - if (!join && !args[0]) { + if (!join && !args.message) { return interaction.reply({ content: 'You need a message for me to say anything!', ephemeral: true }); } else if (!join) { - const body = { guildID: interaction.guild.id, channelID: interaction.channel.id, message: args[0] }; + const body = { guildID: interaction.guild.id, channelID: interaction.channel.id, message: args.message }; await db.joinChannel.create(body); - return interaction.reply({ content: `The join message have been set with ${args[0]}`, ephemeral: true }); + return interaction.reply({ content: `The join message have been set with ${args.message}`, ephemeral: true }); } @@ -49,12 +49,12 @@ export default { if (!interactionMenu.isButton) return; interactionMenu.update({ components: [] }); if (interactionMenu.customId === 'edit') { - if (!args[0]) { + if (!args.message) { return interaction.reply({ content: 'You need to input a message for me to edit!', ephemeral: true }); } - const body = { guildID: interaction.guild.id, channelID: interaction.channel.id, message: args[0] }; + const body = { guildID: interaction.guild.id, channelID: interaction.channel.id, message: args.message }; await db.joinChannel.update(body, { where: { guildID: interaction.guild.id } }); - return interaction.editReply({ content: `The join message has been set to ${args[0]}`, ephemeral: true }); + return interaction.editReply({ content: `The join message has been set to ${args.message}`, ephemeral: true }); } else if (interactionMenu.customId === 'remove') { db.joinChannel.destroy({ where: { guildID: interaction.guild.id, channelID: interaction.channel.id } }); diff --git a/commands/fun/4chan.js b/commands/fun/4chan.js index 305368a0..5dd8090f 100644 --- a/commands/fun/4chan.js +++ b/commands/fun/4chan.js @@ -16,7 +16,7 @@ export default { .setRequired(true)), category: 'fun', async execute(interaction, args) { - let board = args[0]; + let board = args.board; if (fourChan[board] == undefined) { return interaction.reply({ content: 'Uh oh! The board you are looking for does not exist? You think this is a mistake? Please send a feedback telling me so!', ephemeral: true }); diff --git a/commands/fun/fakeuser.js b/commands/fun/fakeuser.js index 8efe1de4..7da72538 100644 --- a/commands/fun/fakeuser.js +++ b/commands/fun/fakeuser.js @@ -20,12 +20,12 @@ export default { clientPermissions: [ PermissionFlagsBits.ManageWebhooks ], async execute(interaction, args) { await interaction.deferReply({ ephemeral: true }); - const member = args[0]; - const message = args[1]; - const attachment = args[2]; + const member = args.user; + const message = args.message; + const attachment = args.image; - - const webhook = await interaction.channel.createWebhook(member.user.username, { + const webhook = await interaction.channel.createWebhook({ + name: member.user.username, avatar: member.user.displayAvatarURL(), reason: `Fakebot/user command triggered by: ${interaction.user.username}`, }); diff --git a/commands/fun/reddit.js b/commands/fun/reddit.js index 3f6de670..99cc40b8 100644 --- a/commands/fun/reddit.js +++ b/commands/fun/reddit.js @@ -13,7 +13,7 @@ export default { category: 'fun', async execute(interaction, args) { await interaction.deferReply({ ephemeral: false }); - const subreddit = args[0]; + const subreddit = args.subreddit; fetch('https://www.reddit.com/r/' + subreddit + '.json?limit=100').then((response) => { return response.json(); }).then((response) => { diff --git a/commands/fun/tweet.js b/commands/fun/tweet.js index a2e857b7..6cceced3 100644 --- a/commands/fun/tweet.js +++ b/commands/fun/tweet.js @@ -27,8 +27,8 @@ export default { ratelimit: 3, cooldown: 3600, async execute(interaction, args, client) { - const content = args[0]; - const attachment = args[1]; + const content = args.content; + const attachment = args.image; if (!content && !attachment) { return interaction.reply({ content: 'Uh oh! You are missing any content for me to tweet!', ephemeral: true }); diff --git a/commands/fun/ytp.js b/commands/fun/ytp.js index 1530c085..602d721f 100644 --- a/commands/fun/ytp.js +++ b/commands/fun/ytp.js @@ -16,7 +16,7 @@ export default { .setRequired(false)), category: 'fun', async execute(interaction, args) { - if (!interaction.channel.nsfw && !args[0]) return interaction.reply(`Please execute this command in an NSFW channel ( Content might not be NSFW but since the video are user submitted better safe than sorry ) OR do \`\`${prefixs[0]}ytp --force\`\` to make the command work outside of nsfw channel BE AWARE THAT IT WON'T CHANGE THE FINAL RESULT SO NSFW CAN STILL HAPPEN`); + if (!interaction.channel.nsfw && !args.force) return interaction.reply(`Please execute this command in an NSFW channel ( Content might not be NSFW but since the video are user submitted better safe than sorry ) OR do \`\`${prefixs[0]}ytp --force\`\` to make the command work outside of nsfw channel BE AWARE THAT IT WON'T CHANGE THE FINAL RESULT SO NSFW CAN STILL HAPPEN`); // Read userVid folder and select random vid and only take .mp4 const mp4 = []; @@ -50,7 +50,7 @@ export default { resources: './asset/ytp/resources/', temp: os.tmpdir(), sourceList: asset, - intro: args[0] ? './asset/ytp/intro.mp4' : null, + intro: args.force ? './asset/ytp/intro.mp4' : null, outro: './asset/ytp/outro.mp4', OUTPUT_FILE: `${os.tmpdir()}/${interaction.id}_YTP.mp4`, MAX_CLIPS: MAX_CLIPS, diff --git a/commands/secret/s.js b/commands/secret/s.js index f075bc51..bd4e4f08 100644 --- a/commands/secret/s.js +++ b/commands/secret/s.js @@ -10,7 +10,7 @@ export default { .setRequired(true)), category: 'secret', async execute(interaction, args) { - const command = args[0]; + const command = args.something; if (command === 'levertowned') { interaction.reply('Hello buddy bro <:youngtroll:488559163832795136> <@434762632004894746>'); diff --git a/commands/utility/download.js b/commands/utility/download.js index c5517822..46c03cc6 100644 --- a/commands/utility/download.js +++ b/commands/utility/download.js @@ -28,9 +28,9 @@ export default { async execute(interaction, args, c) { client = c; - const url = args[0]; - const format = args[1]; - interaction.doCompress = args[2]; + const url = args.url; + const format = args.format; + interaction.doCompress = args.compress; if (interaction.cleanUp) { cleanUp = interaction.cleanUp; } diff --git a/commands/utility/feedback.js b/commands/utility/feedback.js index 77ff7d67..6ed2e8c0 100644 --- a/commands/utility/feedback.js +++ b/commands/utility/feedback.js @@ -18,7 +18,7 @@ export default { .setTimestamp(); if (interaction.guild) Embed.addFields({ name: 'Guild', value: `${interaction.guild.name} (${interaction.guild.id})`, inline: true }); - Embed.addFields({ name: 'Feedback', value: args[0], inline: true }); + Embed.addFields({ name: 'Feedback', value: args.feedback, inline: true }); // Don't let new account use this command to prevent spam const date = new Date(); diff --git a/commands/utility/help.js b/commands/utility/help.js index 4b5324ff..36255258 100644 --- a/commands/utility/help.js +++ b/commands/utility/help.js @@ -7,11 +7,15 @@ const prefixs = prefix.split(','); export default { data: new SlashCommandBuilder() .setName('help') - .setDescription('Displays a list of commands or information about a command.'), + .setDescription('Displays a list of commands or information about a command.') + .addStringOption(option => + option.setName('command') + .setDescription('The command you want more details about.')), category: 'utility', async execute(interaction, args, client) { - if (args[0]) { - const command = client.commands.get(args[0]); + if (args.command) { + const command = client.commands.get(args.command); + if (!command) return interaction.reply(`Did not found any command named \`\`${args.command}\`\`. Please make sure it is a valid command and not an alias.`); const description = Object.assign({ content: 'No description available.', usage: '', diff --git a/commands/utility/vid2gif.js b/commands/utility/vid2gif.js index 5e570708..7e168dd0 100644 --- a/commands/utility/vid2gif.js +++ b/commands/utility/vid2gif.js @@ -17,7 +17,7 @@ export default { category: 'utility', async execute(interaction, args) { await interaction.deferReply({ ephemeral: false }); - const url = args[0]; + const url = args.url; if (!await utils.stringIsAValidurl(url)) { console.error(`Not a url!!! ${url}`); diff --git a/events/client/interactionCreate.js b/events/client/interactionCreate.js index f2c08c07..e3c52cba 100644 --- a/events/client/interactionCreate.js +++ b/events/client/interactionCreate.js @@ -75,15 +75,17 @@ export default { } } - const args = []; + const args = {}; + // https://discord-api-types.dev/api/discord-api-types-v10/enum/ApplicationCommandOptionType interaction.options.data.forEach(arg => { - if (arg.type === 'MENTIONABLE') { - return args.push(arg.member); + let payload = arg.value; + if (arg.type === 9) { + payload = arg.member; } - else if (arg.type === 'ATTACHMENT') { - return args.push(arg.attachment); + else if (arg.type === 11) { + payload = arg.attachment; } - args.push(arg.value); + args[arg.name] = payload; }); await command.execute(interaction, args, client); diff --git a/events/client/messageCreate.js b/events/client/messageCreate.js index d1287180..c382e2c6 100644 --- a/events/client/messageCreate.js +++ b/events/client/messageCreate.js @@ -262,7 +262,7 @@ export default { const messageArray = message.content.match(/"[^"]*"|\S+/g).map(m => m.slice(0, 1) === '"' ? m.slice(1, -1) : m); let commandName = messageArray[1].toLowerCase(); - let messageArgs = messageArray.splice(2, messageArray.length); + const messageArgs = messageArray.splice(2, messageArray.length); // Search for alias client.commands.find(c => { @@ -368,31 +368,21 @@ export default { msg.delete(); }); }; + const args = {}; - if (command.data.options.length > 0) { - // if (command.data.options.length === 1 || command.data.options[command.data.options.length - 1].constructor.name.toLowerCase().includes('attachment')) { - if (command.data.options.length === 1) { - const test = messageArgs.join(' '); - messageArgs = []; - messageArgs.push(test); + for (let i = 0; i < command.data.options.length; i++) { + const arg = command.data.options[i]; + const type = arg.constructor.name.toLowerCase(); + let payload = messageArgs[i]; + if (type.includes('mentionable')) { + payload = message.mentions.members.first(); } - - for (let i = 0; i < messageArgs.length; i++) { - const constructorName = command.data.options[i].constructor.name.toLowerCase(); - if (constructorName.includes('boolean')) { - messageArgs[i] = (messageArgs[i].toLowerCase() === 'true'); - } - - if (constructorName.includes('mentionable')) { - messageArgs[i] = message.mentions.members.first(); - } - } - if (message.attachments) { - messageArgs.push(Array.from(message.attachments.values())[0]); + else if (type.includes('attachment')) { + payload = message.attachments.first(); } + args[arg.name] = payload; } - - await command.execute(message, messageArgs, client); + await command.execute(message, args, client); } catch (error) { console.error(error);