From fda4a091122857a02a485eba0a7546af2884cceb Mon Sep 17 00:00:00 2001 From: Carter Penterman Date: Thu, 12 May 2022 14:56:35 -0500 Subject: [PATCH] added slash command and basic functionality --- README.md | 20 ++++ bot.js | 40 +++++--- commands/take.js | 27 ++++++ deploy-commands.js | 30 ++++++ example_config.json | 1 + package-lock.json | 217 ++++++++++++++++++++++++++++++++++++++------ package.json | 3 + 7 files changed, 298 insertions(+), 40 deletions(-) create mode 100644 README.md create mode 100644 commands/take.js create mode 100644 deploy-commands.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..8f1602c --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +## maptest bot + +to setup (requires node.js): + +* create a copy of example_config.json named config.json +* fill out empty values with correct values in config.json (token is discord token, cookie is .ROBLOSECURITY cookie, clientId is the bot's ID (right click bot in discord then choose copy ID with developer mode enabled)) +* install dependencies +``` +npm install +``` +* register slash commands globally (this only needs to be ran once) +``` +node deploy-commands.js +``` +* run the bot +``` +node bot.js +``` + +also, the bot requires the applications.commands scope, which can be found in developer portal -> bot -> oauth2 -> url generator -> scopes, simply generate link with applications.commands scope checked then add bot to server (can be done if bot already in server) \ No newline at end of file diff --git a/bot.js b/bot.js index ea4ba80..323b82c 100644 --- a/bot.js +++ b/bot.js @@ -1,19 +1,33 @@ -const noblox = require("noblox.js"); -const { Client, Intents } = require("discord.js"); -const {cookie, token} = require("./config.json"); +const fs = require('node:fs'); +const { Client, Collection, Intents } = require("discord.js"); +const {token} = require("./config.json"); -function buyAsset(id) { - noblox.setCookie(cookie) - .then(async () => { - noblox.buy(921233306, 0) - .catch(async (error) => { - console.log("Could not purchase, error: ") - console.log(error) - }); - }); +const client = new Client({ intents: [Intents.FLAGS.GUILDS]}); + +client.commands = new Collection(); +const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); + +for (const file of commandFiles) { + const command = require(`./commands/${file}`); + // Set a new item in the Collection + // With the key as the command name and the value as the exported module + client.commands.set(command.data.name, command); } -const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]}); +client.on('interactionCreate', async interaction => { + if (!interaction.isCommand()) return; + + const command = client.commands.get(interaction.commandName); + + if (!command) return; + + try { + await command.execute(interaction); + } catch (error) { + console.error(error); + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + } +}); client.once("ready", () => { console.log("Ready"); diff --git a/commands/take.js b/commands/take.js new file mode 100644 index 0000000..b9be987 --- /dev/null +++ b/commands/take.js @@ -0,0 +1,27 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const noblox = require("noblox.js"); +const {cookie} = require("../config.json"); + +module.exports = { + data: new SlashCommandBuilder() + .setName('take') + .setDescription('Takes an asset ID') + .addIntegerOption(option => + option.setName("asset_id") + .setDescription("The asset ID of the model") + .setRequired(true)) + , + async execute(interaction) { + const id = interaction.options.getInteger("asset_id"); + await noblox.setCookie(cookie) + .then(async () => { + noblox.buy(id, 0) + .catch(async (error) => { + await interaction.reply("Could not take model!"); + console.log("Could not take model: ") + console.log(error) + }); + }); + await interaction.reply("Model was taken successfully!"); + }, +}; \ No newline at end of file diff --git a/deploy-commands.js b/deploy-commands.js new file mode 100644 index 0000000..6c1d17b --- /dev/null +++ b/deploy-commands.js @@ -0,0 +1,30 @@ +const { REST } = require('@discordjs/rest'); +const { Routes } = require('discord-api-types/v9'); +const { clientId, token } = require('./config.json'); +const fs = require('node:fs'); + +const commands = []; +const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); + +for (const file of commandFiles) { + const command = require(`./commands/${file}`); + commands.push(command.data.toJSON()); +} + +const rest = new REST({ version: '9' }).setToken(token); + +(async () => { + try { + console.log('Started refreshing application (/) commands.'); + + await rest.put( + Routes.applicationCommands(clientId), + { body: commands }, + ); + + console.log('Successfully reloaded application (/) commands.'); + + } catch (error) { + console.error(error); + } +})(); \ No newline at end of file diff --git a/example_config.json b/example_config.json index 48bbbb1..7b54dd8 100644 --- a/example_config.json +++ b/example_config.json @@ -1,4 +1,5 @@ { "token": "", + "clientId": "", "cookie": "" } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index f077a2f..5116cbc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,26 +5,34 @@ "packages": { "": { "dependencies": { + "@discordjs/builders": "^0.13.0", + "@discordjs/rest": "^0.4.1", + "discord-api-types": "^0.32.1", "discord.js": "^13.6.0", "noblox.js": "^4.13.1" } }, "node_modules/@discordjs/builders": { - "version": "0.11.0", - "resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-0.11.0.tgz", - "integrity": "sha512-ZTB8yJdJKrKlq44dpWkNUrAtEJEq0gqpb7ASdv4vmq6/mZal5kOv312hQ56I/vxwMre+VIkoHquNUAfnTbiYtg==", + "version": "0.13.0", + "resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-0.13.0.tgz", + "integrity": "sha512-4L9y26KRNNU8Y7J78SRUN1Uhava9D8jfit/YqEaKi8gQRc7PdqKqk2poybo6RXaiyt/BgKYPfcjxT7WvzGfYCA==", "dependencies": { - "@sindresorhus/is": "^4.2.0", - "discord-api-types": "^0.26.0", - "ts-mixer": "^6.0.0", - "tslib": "^2.3.1", - "zod": "^3.11.6" + "@sapphire/shapeshift": "^2.0.0", + "@sindresorhus/is": "^4.6.0", + "discord-api-types": "^0.31.1", + "fast-deep-equal": "^3.1.3", + "ts-mixer": "^6.0.1", + "tslib": "^2.3.1" }, "engines": { - "node": ">=16.0.0", - "npm": ">=7.0.0" + "node": ">=16.9.0" } }, + "node_modules/@discordjs/builders/node_modules/discord-api-types": { + "version": "0.31.2", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.31.2.tgz", + "integrity": "sha512-gpzXTvFVg7AjKVVJFH0oJGC0q0tO34iJGSHZNz9u3aqLxlD6LfxEs9wWVVikJqn9gra940oUTaPFizCkRDcEiA==" + }, "node_modules/@discordjs/collection": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-0.4.0.tgz", @@ -34,6 +42,50 @@ "npm": ">=7.0.0" } }, + "node_modules/@discordjs/rest": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@discordjs/rest/-/rest-0.4.1.tgz", + "integrity": "sha512-rtWy+AIfNlfjGkAgA2TJLASdqli07aTNQceVGT6RQQiQaEqV0nsfBO4WtDlDzk7PmO3w+InP3dpwEolJI5jz0A==", + "dependencies": { + "@discordjs/collection": "^0.7.0-dev", + "@sapphire/async-queue": "^1.3.1", + "@sapphire/snowflake": "^3.2.1", + "@types/node-fetch": "^2.6.1", + "discord-api-types": "^0.29.0", + "form-data": "^4.0.0", + "node-fetch": "^2.6.7", + "tslib": "^2.3.1" + }, + "engines": { + "node": ">=16.9.0" + } + }, + "node_modules/@discordjs/rest/node_modules/@discordjs/collection": { + "version": "0.7.0-dev.1652350334-4515a1e", + "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-0.7.0-dev.1652350334-4515a1e.tgz", + "integrity": "sha512-fBlyAUGP2Rhc083eLVniSMJSDV76c9YdgEK4gdfw5F+7T1PlixEomo5su+XhuwhMa5VIhPT1FfC3vsQAFg4ZQA==", + "engines": { + "node": ">=16.9.0" + } + }, + "node_modules/@discordjs/rest/node_modules/discord-api-types": { + "version": "0.29.0", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.29.0.tgz", + "integrity": "sha512-Ekq1ICNpOTVajXKZguNFrsDeTmam+ZeA38txsNLZnANdXUjU6QBPIZLUQTC6MzigFGb0Tt8vk4xLnXmzv0shNg==" + }, + "node_modules/@discordjs/rest/node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/@sapphire/async-queue": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/@sapphire/async-queue/-/async-queue-1.3.1.tgz", @@ -43,6 +95,24 @@ "npm": ">=7.0.0" } }, + "node_modules/@sapphire/shapeshift": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@sapphire/shapeshift/-/shapeshift-2.2.0.tgz", + "integrity": "sha512-UEnKgMlQyI0yY/q+lCMX0VJft9y86IsesgbIQj6e62FBYSaMVr+IaMNpi4z45Q14VnuMACbK0yrbHISNqgUYcQ==", + "engines": { + "node": ">=v15.0.0", + "npm": ">=7.0.0" + } + }, + "node_modules/@sapphire/snowflake": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/@sapphire/snowflake/-/snowflake-3.2.2.tgz", + "integrity": "sha512-ula2O0kpSZtX9rKXNeQMrHwNd7E4jPDJYUXmEGTFdMRfyfMw+FPyh04oKMjAiDuOi64bYgVkOV3MjK+loImFhQ==", + "engines": { + "node": ">=v14.0.0", + "npm": ">=7.0.0" + } + }, "node_modules/@sindresorhus/is": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.6.0.tgz", @@ -332,12 +402,9 @@ } }, "node_modules/discord-api-types": { - "version": "0.26.1", - "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.26.1.tgz", - "integrity": "sha512-T5PdMQ+Y1MEECYMV5wmyi9VEYPagEDEi4S0amgsszpWY0VB9JJ/hEvM6BgLhbdnKky4gfmZEXtEEtojN8ZKJQQ==", - "engines": { - "node": ">=12" - } + "version": "0.32.1", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.32.1.tgz", + "integrity": "sha512-/ewl0CPYT5xjOC+SJ7wADJKjtpZfiiUaYXOP/Ns54lnBcv4Xqa4iKSqRF/w1fjiUvWTYN9W8UuOiyCHtmu5fJw==" }, "node_modules/discord.js": { "version": "13.6.0", @@ -359,6 +426,30 @@ "npm": ">=7.0.0" } }, + "node_modules/discord.js/node_modules/@discordjs/builders": { + "version": "0.11.0", + "resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-0.11.0.tgz", + "integrity": "sha512-ZTB8yJdJKrKlq44dpWkNUrAtEJEq0gqpb7ASdv4vmq6/mZal5kOv312hQ56I/vxwMre+VIkoHquNUAfnTbiYtg==", + "dependencies": { + "@sindresorhus/is": "^4.2.0", + "discord-api-types": "^0.26.0", + "ts-mixer": "^6.0.0", + "tslib": "^2.3.1", + "zod": "^3.11.6" + }, + "engines": { + "node": ">=16.0.0", + "npm": ">=7.0.0" + } + }, + "node_modules/discord.js/node_modules/discord-api-types": { + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.26.1.tgz", + "integrity": "sha512-T5PdMQ+Y1MEECYMV5wmyi9VEYPagEDEi4S0amgsszpWY0VB9JJ/hEvM6BgLhbdnKky4gfmZEXtEEtojN8ZKJQQ==", + "engines": { + "node": ">=12" + } + }, "node_modules/discord.js/node_modules/form-data": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", @@ -1118,15 +1209,23 @@ }, "dependencies": { "@discordjs/builders": { - "version": "0.11.0", - "resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-0.11.0.tgz", - "integrity": "sha512-ZTB8yJdJKrKlq44dpWkNUrAtEJEq0gqpb7ASdv4vmq6/mZal5kOv312hQ56I/vxwMre+VIkoHquNUAfnTbiYtg==", + "version": "0.13.0", + "resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-0.13.0.tgz", + "integrity": "sha512-4L9y26KRNNU8Y7J78SRUN1Uhava9D8jfit/YqEaKi8gQRc7PdqKqk2poybo6RXaiyt/BgKYPfcjxT7WvzGfYCA==", "requires": { - "@sindresorhus/is": "^4.2.0", - "discord-api-types": "^0.26.0", - "ts-mixer": "^6.0.0", - "tslib": "^2.3.1", - "zod": "^3.11.6" + "@sapphire/shapeshift": "^2.0.0", + "@sindresorhus/is": "^4.6.0", + "discord-api-types": "^0.31.1", + "fast-deep-equal": "^3.1.3", + "ts-mixer": "^6.0.1", + "tslib": "^2.3.1" + }, + "dependencies": { + "discord-api-types": { + "version": "0.31.2", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.31.2.tgz", + "integrity": "sha512-gpzXTvFVg7AjKVVJFH0oJGC0q0tO34iJGSHZNz9u3aqLxlD6LfxEs9wWVVikJqn9gra940oUTaPFizCkRDcEiA==" + } } }, "@discordjs/collection": { @@ -1134,11 +1233,58 @@ "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-0.4.0.tgz", "integrity": "sha512-zmjq+l/rV35kE6zRrwe8BHqV78JvIh2ybJeZavBi5NySjWXqN3hmmAKg7kYMMXSeiWtSsMoZ/+MQi0DiQWy2lw==" }, + "@discordjs/rest": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@discordjs/rest/-/rest-0.4.1.tgz", + "integrity": "sha512-rtWy+AIfNlfjGkAgA2TJLASdqli07aTNQceVGT6RQQiQaEqV0nsfBO4WtDlDzk7PmO3w+InP3dpwEolJI5jz0A==", + "requires": { + "@discordjs/collection": "^0.7.0-dev", + "@sapphire/async-queue": "^1.3.1", + "@sapphire/snowflake": "^3.2.1", + "@types/node-fetch": "^2.6.1", + "discord-api-types": "^0.29.0", + "form-data": "^4.0.0", + "node-fetch": "^2.6.7", + "tslib": "^2.3.1" + }, + "dependencies": { + "@discordjs/collection": { + "version": "0.7.0-dev.1652350334-4515a1e", + "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-0.7.0-dev.1652350334-4515a1e.tgz", + "integrity": "sha512-fBlyAUGP2Rhc083eLVniSMJSDV76c9YdgEK4gdfw5F+7T1PlixEomo5su+XhuwhMa5VIhPT1FfC3vsQAFg4ZQA==" + }, + "discord-api-types": { + "version": "0.29.0", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.29.0.tgz", + "integrity": "sha512-Ekq1ICNpOTVajXKZguNFrsDeTmam+ZeA38txsNLZnANdXUjU6QBPIZLUQTC6MzigFGb0Tt8vk4xLnXmzv0shNg==" + }, + "form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + } + } + }, "@sapphire/async-queue": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/@sapphire/async-queue/-/async-queue-1.3.1.tgz", "integrity": "sha512-FFTlPOWZX1kDj9xCAsRzH5xEJfawg1lNoYAA+ecOWJMHOfiZYb1uXOI3ne9U4UILSEPwfE68p3T9wUHwIQfR0g==" }, + "@sapphire/shapeshift": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@sapphire/shapeshift/-/shapeshift-2.2.0.tgz", + "integrity": "sha512-UEnKgMlQyI0yY/q+lCMX0VJft9y86IsesgbIQj6e62FBYSaMVr+IaMNpi4z45Q14VnuMACbK0yrbHISNqgUYcQ==" + }, + "@sapphire/snowflake": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/@sapphire/snowflake/-/snowflake-3.2.2.tgz", + "integrity": "sha512-ula2O0kpSZtX9rKXNeQMrHwNd7E4jPDJYUXmEGTFdMRfyfMw+FPyh04oKMjAiDuOi64bYgVkOV3MjK+loImFhQ==" + }, "@sindresorhus/is": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.6.0.tgz", @@ -1365,9 +1511,9 @@ "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" }, "discord-api-types": { - "version": "0.26.1", - "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.26.1.tgz", - "integrity": "sha512-T5PdMQ+Y1MEECYMV5wmyi9VEYPagEDEi4S0amgsszpWY0VB9JJ/hEvM6BgLhbdnKky4gfmZEXtEEtojN8ZKJQQ==" + "version": "0.32.1", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.32.1.tgz", + "integrity": "sha512-/ewl0CPYT5xjOC+SJ7wADJKjtpZfiiUaYXOP/Ns54lnBcv4Xqa4iKSqRF/w1fjiUvWTYN9W8UuOiyCHtmu5fJw==" }, "discord.js": { "version": "13.6.0", @@ -1385,6 +1531,23 @@ "ws": "^8.4.0" }, "dependencies": { + "@discordjs/builders": { + "version": "0.11.0", + "resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-0.11.0.tgz", + "integrity": "sha512-ZTB8yJdJKrKlq44dpWkNUrAtEJEq0gqpb7ASdv4vmq6/mZal5kOv312hQ56I/vxwMre+VIkoHquNUAfnTbiYtg==", + "requires": { + "@sindresorhus/is": "^4.2.0", + "discord-api-types": "^0.26.0", + "ts-mixer": "^6.0.0", + "tslib": "^2.3.1", + "zod": "^3.11.6" + } + }, + "discord-api-types": { + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.26.1.tgz", + "integrity": "sha512-T5PdMQ+Y1MEECYMV5wmyi9VEYPagEDEi4S0amgsszpWY0VB9JJ/hEvM6BgLhbdnKky4gfmZEXtEEtojN8ZKJQQ==" + }, "form-data": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", diff --git a/package.json b/package.json index 2e2653f..b8be540 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,8 @@ { "dependencies": { + "@discordjs/builders": "^0.13.0", + "@discordjs/rest": "^0.4.1", + "discord-api-types": "^0.32.1", "discord.js": "^13.6.0", "noblox.js": "^4.13.1" }