From e3318d26c79abd2ffd6bf577a088cfc260ca536a Mon Sep 17 00:00:00 2001 From: Nathaniel Mason Date: Fri, 22 Apr 2022 02:53:03 +0100 Subject: [PATCH] use the new client.application.commands to generate slash command interfaces --- commands/8ball.js | 9 ++++++++- commands/avatar.js | 14 ++++++++++++++ commands/bonk.js | 8 ++++++++ commands/cheese.js | 10 +++++++++- commands/choose.js | 1 + commands/contributors.js | 1 + commands/cuddle.js | 8 ++++++++ commands/eval.js | 8 ++++++++ commands/help.js | 2 +- commands/hug.js | 8 ++++++++ commands/info.js | 1 + commands/invite.js | 2 +- commands/kiss.js | 8 ++++++++ commands/leskiss.js | 8 ++++++++ commands/lick.js | 8 ++++++++ commands/nom.js | 8 ++++++++ commands/pat.js | 8 ++++++++ commands/ping.js | 1 + commands/poke.js | 8 ++++++++ commands/reload.js | 1 + commands/restart.js | 1 + commands/setnick.js | 8 ++++++++ commands/slap.js | 8 ++++++++ commands/snuggle.js | 8 ++++++++ commands/stop.js | 1 + commands/uinfo.js | 15 ++++++++++++++- start.js | 34 ++++++++++++++++++++++++++-------- 27 files changed, 184 insertions(+), 13 deletions(-) diff --git a/commands/8ball.js b/commands/8ball.js index f196c63..668cf31 100644 --- a/commands/8ball.js +++ b/commands/8ball.js @@ -1,8 +1,15 @@ +const { Constants } = require('discord.js'); + module.exports = { name: require('path').parse(__filename).name, description: 'Ask Anitrox a question, any question! and they will answer it!', - syntax: ['[Question]'], + options: [{ + name: 'question', + description: 'The question to ask Anitrox', + required: true, + type: Constants.ApplicationCommandOptionTypes.STRING + }], async execute (client, message, args, config) { const index = Math.floor(Math.random() * config.answers.length); diff --git a/commands/avatar.js b/commands/avatar.js index 16159ef..ee831cd 100644 --- a/commands/avatar.js +++ b/commands/avatar.js @@ -1,7 +1,21 @@ +const { Constants } = require('discord.js'); + module.exports = { name: require('path').parse(__filename).name, description: "Gets a user's avatar.", + options: [{ + name: 'user', + description: 'Another user', + required: false, + type: Constants.ApplicationCommandOptionTypes.USER + }, + { + name: 'userid', + description: "Another user's ID", + required: false, + type: Constants.ApplicationCommandOptionTypes.INTEGER + }], async execute (client, message, args, config) { const user = message.mentions.users.first() || client.users.cache.get(args[0]) || message.author; diff --git a/commands/bonk.js b/commands/bonk.js index bc4ffb5..e1088ac 100644 --- a/commands/bonk.js +++ b/commands/bonk.js @@ -1,7 +1,15 @@ +const { Constants } = require('discord.js'); + module.exports = { name: require('path').parse(__filename).name, description: 'Bonks a user!', + options: [{ + name: 'user', + description: 'The user to bonk', + required: true, + type: Constants.ApplicationCommandOptionTypes.USER + }], async execute (client, message, _, config) { const taggedUser = message.mentions.users.first(); diff --git a/commands/cheese.js b/commands/cheese.js index 94cc4a9..4721efc 100644 --- a/commands/cheese.js +++ b/commands/cheese.js @@ -1,7 +1,15 @@ +const { Constants } = require('discord.js'); + module.exports = { name: require('path').parse(__filename).name, - description: 'Cheese an user, or run just ``n!cheese`` for a surprise :eyes:', + description: 'Cheese a user, or run with no arguments for a surprise :eyes:', + options: [{ + name: 'user', + description: 'The user to cheese', + required: false, + type: Constants.ApplicationCommandOptionTypes.USER + }], async execute (_0, message, _1, config) { const taggedUser = message.mentions.users.first(); diff --git a/commands/choose.js b/commands/choose.js index bb754e3..083b518 100644 --- a/commands/choose.js +++ b/commands/choose.js @@ -2,6 +2,7 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Give some lines of input, and get one back at random', + options: [], async execute (client, message, _, config) { const avatarURL = message.author.displayAvatarURL(); diff --git a/commands/contributors.js b/commands/contributors.js index 3538684..17e8c9a 100644 --- a/commands/contributors.js +++ b/commands/contributors.js @@ -2,6 +2,7 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Attributions to open source components used by Anitrox', + options: [], async execute (_0, message, _1, config) { await message.channel.send({ diff --git a/commands/cuddle.js b/commands/cuddle.js index 7c6466a..1beac8e 100644 --- a/commands/cuddle.js +++ b/commands/cuddle.js @@ -1,3 +1,5 @@ +const { Constants } = require('discord.js'); + const gifchoices = [ 'https://i.pinimg.com/originals/4d/89/d7/4d89d7f963b41a416ec8a55230dab31b.gif', 'https://media1.tenor.com/images/6d73b0a9cadef5310be4b6160d2f959a/tenor.gif?itemid=12099823', @@ -9,6 +11,12 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Cuddle a user!', + options: [{ + name: 'user', + description: 'The user to cuddle', + required: true, + type: Constants.ApplicationCommandOptionTypes.USER + }], async execute (client, message, _, config) { const taggedUser = message.mentions.users.first(); diff --git a/commands/eval.js b/commands/eval.js index d288c7d..5a2493f 100644 --- a/commands/eval.js +++ b/commands/eval.js @@ -1,9 +1,17 @@ +const { Constants } = require('discord.js'); + const { inspect } = require('util'); module.exports = { name: require('path').parse(__filename).name, description: 'Executes JS code', + options: [{ + name: 'text', + description: 'The string to evaluate', + required: true, + type: Constants.ApplicationCommandOptionTypes.STRING + }], async execute (_, message, args, config) { if (message.author.id === config.ownerID) { diff --git a/commands/help.js b/commands/help.js index 94b9f76..a62e9e5 100644 --- a/commands/help.js +++ b/commands/help.js @@ -2,7 +2,7 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Get help on anything from commands, to what the bot does! just not your homework..', - syntax: '', + options: [], async execute (_0, message, _1, config) { await message.channel.send({ diff --git a/commands/hug.js b/commands/hug.js index 5ded0cd..be6a8ad 100644 --- a/commands/hug.js +++ b/commands/hug.js @@ -1,3 +1,5 @@ +const { Constants } = require('discord.js'); + const gifchoices = [ 'https://cdn.discordapp.com/attachments/803658122299572255/807670647920001044/hug2.gif', 'https://cdn.discordapp.com/attachments/803658122299572255/807670797983285268/hug1.gif', @@ -10,6 +12,12 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Hugs a user!', + options: [{ + name: 'user', + description: 'The user to hug', + required: true, + type: Constants.ApplicationCommandOptionTypes.USER + }], async execute (client, message, _, config) { const taggedUser = message.mentions.users.first(); diff --git a/commands/info.js b/commands/info.js index 930b86b..f99181d 100644 --- a/commands/info.js +++ b/commands/info.js @@ -18,6 +18,7 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Shows bot and host information', + options: [], async execute (client, message, _, config) { const os = require('os'); diff --git a/commands/invite.js b/commands/invite.js index e373fbf..76e85b1 100644 --- a/commands/invite.js +++ b/commands/invite.js @@ -2,7 +2,7 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Add Anitrox to your beautiful server!', - syntax: [], + options: [], async execute (_0, message, _1, config) { await message.channel.send({ diff --git a/commands/kiss.js b/commands/kiss.js index e79bfd9..e36d0cc 100644 --- a/commands/kiss.js +++ b/commands/kiss.js @@ -1,3 +1,5 @@ +const { Constants } = require('discord.js'); + const gifchoices = [ 'https://cdn.discordapp.com/attachments/803658122299572255/807671954055626812/kiss5.gif', 'https://cdn.discordapp.com/attachments/803658122299572255/807671956236140554/kiss2.gif', @@ -10,6 +12,12 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Kisses a user!', + options: [{ + name: 'user', + description: 'The user to kiss', + required: true, + type: Constants.ApplicationCommandOptionTypes.USER + }], async execute (client, message, _, config) { const taggedUser = message.mentions.users.first(); diff --git a/commands/leskiss.js b/commands/leskiss.js index 1458094..8758203 100644 --- a/commands/leskiss.js +++ b/commands/leskiss.js @@ -1,3 +1,5 @@ +const { Constants } = require('discord.js'); + const gifchoices = [ 'https://cdn.discordapp.com/attachments/793537380330111028/803833954750038066/gif5.gif', 'https://cdn.discordapp.com/attachments/793537380330111028/803833959338475550/gif12.gif', @@ -20,6 +22,12 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Lesbian kiss <:lesbian:803831629428686849>', + options: [{ + name: 'user', + description: 'The user to kiss', + required: true, + type: Constants.ApplicationCommandOptionTypes.USER + }], async execute (client, message, _, config) { const taggedUser = message.mentions.users.first(); diff --git a/commands/lick.js b/commands/lick.js index f4490b5..85d6de3 100644 --- a/commands/lick.js +++ b/commands/lick.js @@ -1,3 +1,5 @@ +const { Constants } = require('discord.js'); + const gifchoices = [ 'https://cdn.discordapp.com/attachments/803658122299572255/805314244123951114/cef569820773b0f5d54ee34cfa18e1f8.gif', 'https://cdn.lowgif.com/full/2027501b8fa5225c-.gif', @@ -9,6 +11,12 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Licks a user!', + options: [{ + name: 'user', + description: 'The user to lick', + required: true, + type: Constants.ApplicationCommandOptionTypes.USER + }], async execute (client, message, _, config) { const taggedUser = message.mentions.users.first(); diff --git a/commands/nom.js b/commands/nom.js index fc63286..8f319e7 100644 --- a/commands/nom.js +++ b/commands/nom.js @@ -1,3 +1,5 @@ +const { Constants } = require('discord.js'); + const gifchoices = [ 'https://i.imgur.com/Ns1RBzX.gif', 'https://cdn.lowgif.com/full/2027501b8fa5225c-.gif', @@ -9,6 +11,12 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Noms an user!', + options: [{ + name: 'user', + description: 'The user to nom', + required: true, + type: Constants.ApplicationCommandOptionTypes.USER + }], async execute (client, message, _, config) { const taggedUser = message.mentions.users.first(); diff --git a/commands/pat.js b/commands/pat.js index eecaea4..4e843a7 100644 --- a/commands/pat.js +++ b/commands/pat.js @@ -1,3 +1,5 @@ +const { Constants } = require('discord.js'); + const gifchoices = [ 'https://cdn.discordapp.com/attachments/803658122299572255/803708174293008474/tenor.gif', 'https://community.gamepress.gg/uploads/default/original/3X/0/a/0a762099c5ad6de9ca5f13dd22a7e45884a99eb3.gif', @@ -8,6 +10,12 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Pats a user!', + options: [{ + name: 'user', + description: 'The user to pat', + required: true, + type: Constants.ApplicationCommandOptionTypes.USER + }], async execute (client, message, _, config) { const taggedUser = message.mentions.users.first(); diff --git a/commands/ping.js b/commands/ping.js index bbed30c..5163a4e 100644 --- a/commands/ping.js +++ b/commands/ping.js @@ -2,6 +2,7 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Gets bot ping', + options: [], async execute (client, message, _, config) { const index = Math.floor(Math.random() * config.locations.length); diff --git a/commands/poke.js b/commands/poke.js index 903e45a..830c435 100644 --- a/commands/poke.js +++ b/commands/poke.js @@ -1,3 +1,5 @@ +const { Constants } = require('discord.js'); + const gifchoices = [ 'https://i.pinimg.com/originals/b4/95/fb/b495fb19f4b9a1b04f48297b676c497b.gif', 'https://i.imgur.com/H7Ok5tn.gif', @@ -8,6 +10,12 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Pokes a user!', + options: [{ + name: 'user', + description: 'The user to poke', + required: true, + type: Constants.ApplicationCommandOptionTypes.USER + }], async execute (client, message, _, config) { const taggedUser = message.mentions.users.first(); diff --git a/commands/reload.js b/commands/reload.js index b4434d9..3bee5af 100644 --- a/commands/reload.js +++ b/commands/reload.js @@ -2,6 +2,7 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Reloads a command', + options: [], async execute (client, message, args, config) { const avatarURL = message.author.displayAvatarURL(); diff --git a/commands/restart.js b/commands/restart.js index 8006701..09482cd 100644 --- a/commands/restart.js +++ b/commands/restart.js @@ -2,6 +2,7 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Restarts the bot', + options: [], async execute (client, message, _, config) { if (message.author.id === config.ownerID) { diff --git a/commands/setnick.js b/commands/setnick.js index d63e0c5..ca71b01 100644 --- a/commands/setnick.js +++ b/commands/setnick.js @@ -1,7 +1,15 @@ +const { Constants } = require('discord.js'); + module.exports = { name: require('path').parse(__filename).name, description: 'Sets your nickname', + options: [{ + name: 'name', + description: 'The new nickname', + required: true, + type: Constants.ApplicationCommandOptionTypes.STRING + }], async execute (client, message, args, config) { const avatarURL = message.author.displayAvatarURL(); diff --git a/commands/slap.js b/commands/slap.js index 9146866..2b0beb5 100644 --- a/commands/slap.js +++ b/commands/slap.js @@ -1,7 +1,15 @@ +const { Constants } = require('discord.js'); + module.exports = { name: require('path').parse(__filename).name, description: 'Slaps an user!', + options: [{ + name: 'user', + description: 'The user to slap', + required: true, + type: Constants.ApplicationCommandOptionTypes.USER + }], async execute (client, message, _, config) { const taggedUser = message.mentions.users.first(); diff --git a/commands/snuggle.js b/commands/snuggle.js index 4322d9f..4c54797 100644 --- a/commands/snuggle.js +++ b/commands/snuggle.js @@ -1,3 +1,5 @@ +const { Constants } = require('discord.js'); + const gifchoices = [ 'https://media.discordapp.net/attachments/803658122299572255/806775382995894282/anime-couple-snuggle-gif-5.gif?width=450&height=238', 'https://media.discordapp.net/attachments/803658122299572255/806775411928989726/snuggl1.gif', @@ -8,6 +10,12 @@ module.exports = { name: require('path').parse(__filename).name, description: 'Snuggle an user!', + options: [{ + name: 'user', + description: 'The user to snuggle', + required: true, + type: Constants.ApplicationCommandOptionTypes.USER + }], async execute (client, message, _, config) { const taggedUser = message.mentions.users.first(); diff --git a/commands/stop.js b/commands/stop.js index f878c80..d98d512 100644 --- a/commands/stop.js +++ b/commands/stop.js @@ -2,6 +2,7 @@ module.exports = { name: require('path').parse(__filename).name, description: "IT'S TIME TO STOP!... the bot", + options: [], async execute (_0, message, _1, config) { if (message.author.id === config.ownerID) { diff --git a/commands/uinfo.js b/commands/uinfo.js index 2ed0b0f..383d94f 100644 --- a/commands/uinfo.js +++ b/commands/uinfo.js @@ -1,8 +1,21 @@ +const { Constants } = require('discord.js'); + module.exports = { name: require('path').parse(__filename).name, description: 'Gets info about an user, such as ID, Discord Join date and more', - syntax: '', + options: [{ + name: 'user', + description: 'Another user', + required: false, + type: Constants.ApplicationCommandOptionTypes.USER + }, + { + name: 'userid', + description: "Another user's ID", + required: false, + type: Constants.ApplicationCommandOptionTypes.INTEGER + }], async execute (_0, message, _1, config) { const user = message.mentions.users.first() || message.member; diff --git a/start.js b/start.js index 778ee15..9fe1db4 100755 --- a/start.js +++ b/start.js @@ -5,14 +5,12 @@ const Discord = require('discord.js'); const config = require('./config.json'); console.log('Starting!'); const client = new Discord.Client({ intents: config.intents.map(intent => eval(`Discord.Intents.FLAGS.${intent}`)) }); + client.commands = new Discord.Collection(); - -const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); - -for (const file of commandFiles) { +fs.readdirSync('./commands').filter(file => file.endsWith('.js')).forEach(file => { const command = require(`./commands/${file}`); client.commands.set(command.name, command); -} +}); client.generateErrorMessage = (errorMsg, avatarURL) => ({ embeds: [{ @@ -33,8 +31,28 @@ client.generateErrorMessage = (errorMsg, avatarURL) => ({ client.on('error', (e) => console.log(`[ERROR] ${e}`)); client.on('warn', (e) => (`[WARN] ${e}`)); -client.once('ready', () => { - console.clear(); +client.once('ready', async () => { + const commands = config.sandbox ? client.guilds.cache.get(config.sandboxGuild)?.commands : client.application.commands; + + if (config.sandbox) { + console.log('deleting previous commands from sandbox'); + const localCommands = await commands.fetch(); + localCommands.forEach(async x => { + await commands.delete(x); + }); + + // console.log('deleting global commands'); + // const globalCommands = await client.application.commands.fetch(); + // globalCommands.forEach(async x => { + // await client.application.commands.delete(x); + // }); + } + + client.commands.forEach(async command => { + await commands.create(command); + }); + + // console.clear(); console.log(' ___ _ __ '); console.log(' / | ____ (_) /__________ _ __'); console.log(' / /| | / __ \\/ / __/ ___/ __ \\| |/_/'); @@ -62,7 +80,7 @@ client.on('messageCreate', async (message) => { try { await client.commands.get(command).execute(client, message, args, config); } catch (error) { - console.stack(); + console.trace(); message.channel.send({ embeds: [{ title: '<:AnitroxError:809651936563429416> **Something went wrong!**',