From 18efa80dc29d24005cfb80eb83c355ff08b1e336 Mon Sep 17 00:00:00 2001 From: notunderctrl <119873044+notunderctrl@users.noreply.github.com> Date: Wed, 21 Dec 2022 21:00:44 +0300 Subject: [PATCH] Initial commit - Part 1, Part 2, Part 3 --- 01 - Setting up project/.env.example | 1 + 01 - Setting up project/package.json | 16 ++++++ 01 - Setting up project/src/index.js | 27 +++++++++ 02 - Registering Slash Commands/.env.example | 3 + 02 - Registering Slash Commands/package.json | 16 ++++++ 02 - Registering Slash Commands/src/index.js | 29 ++++++++++ .../src/register-commands.js | 33 +++++++++++ 03 - Options and Choices/.env.example | 3 + 03 - Options and Choices/package.json | 16 ++++++ 03 - Options and Choices/src/index.js | 28 +++++++++ .../src/register-commands.js | 57 +++++++++++++++++++ 11 files changed, 229 insertions(+) create mode 100644 01 - Setting up project/.env.example create mode 100644 01 - Setting up project/package.json create mode 100644 01 - Setting up project/src/index.js create mode 100644 02 - Registering Slash Commands/.env.example create mode 100644 02 - Registering Slash Commands/package.json create mode 100644 02 - Registering Slash Commands/src/index.js create mode 100644 02 - Registering Slash Commands/src/register-commands.js create mode 100644 03 - Options and Choices/.env.example create mode 100644 03 - Options and Choices/package.json create mode 100644 03 - Options and Choices/src/index.js create mode 100644 03 - Options and Choices/src/register-commands.js diff --git a/01 - Setting up project/.env.example b/01 - Setting up project/.env.example new file mode 100644 index 0000000..36db7ed --- /dev/null +++ b/01 - Setting up project/.env.example @@ -0,0 +1 @@ +TOKEN = YOUR_BOT_TOKEN \ No newline at end of file diff --git a/01 - Setting up project/package.json b/01 - Setting up project/package.json new file mode 100644 index 0000000..6710c7b --- /dev/null +++ b/01 - Setting up project/package.json @@ -0,0 +1,16 @@ +{ + "name": "discord-bot", + "version": "1.0.0", + "description": "", + "main": "src/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "discord.js": "^14.7.1", + "dotenv": "^16.0.3" + } +} diff --git a/01 - Setting up project/src/index.js b/01 - Setting up project/src/index.js new file mode 100644 index 0000000..6e99ee8 --- /dev/null +++ b/01 - Setting up project/src/index.js @@ -0,0 +1,27 @@ +require('dotenv').config(); +const { Client, IntentsBitField } = require('discord.js'); + +const client = new Client({ + intents: [ + IntentsBitField.Flags.Guilds, + IntentsBitField.Flags.GuildMembers, + IntentsBitField.Flags.GuildMessages, + IntentsBitField.Flags.MessageContent, + ], +}); + +client.on('ready', (c) => { + console.log(`✅ ${c.user.tag} is online.`); +}); + +client.on('messageCreate', (message) => { + if (message.author.bot) { + return; + } + + if (message.content === 'hello') { + message.reply('hello'); + } +}); + +client.login(process.env.TOKEN); diff --git a/02 - Registering Slash Commands/.env.example b/02 - Registering Slash Commands/.env.example new file mode 100644 index 0000000..ac59ed5 --- /dev/null +++ b/02 - Registering Slash Commands/.env.example @@ -0,0 +1,3 @@ +TOKEN = YOUR_BOT_TOKEN +CLIENT_ID = YOUR_BOT_ID +GUILD_ID = YOUR_SERVER_ID \ No newline at end of file diff --git a/02 - Registering Slash Commands/package.json b/02 - Registering Slash Commands/package.json new file mode 100644 index 0000000..6710c7b --- /dev/null +++ b/02 - Registering Slash Commands/package.json @@ -0,0 +1,16 @@ +{ + "name": "discord-bot", + "version": "1.0.0", + "description": "", + "main": "src/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "discord.js": "^14.7.1", + "dotenv": "^16.0.3" + } +} diff --git a/02 - Registering Slash Commands/src/index.js b/02 - Registering Slash Commands/src/index.js new file mode 100644 index 0000000..32e19ac --- /dev/null +++ b/02 - Registering Slash Commands/src/index.js @@ -0,0 +1,29 @@ +require('dotenv').config(); +const { Client, IntentsBitField } = require('discord.js'); + +const client = new Client({ + intents: [ + IntentsBitField.Flags.Guilds, + IntentsBitField.Flags.GuildMembers, + IntentsBitField.Flags.GuildMessages, + IntentsBitField.Flags.MessageContent, + ], +}); + +client.on('ready', (c) => { + console.log(`✅ ${c.user.tag} is online.`); +}); + +client.on('interactionCreate', (interaction) => { + if (!interaction.isChatInputCommand()) return; + + if (interaction.commandName === 'hey') { + return interaction.reply('hey!'); + } + + if (interaction.commandName === 'ping') { + return interaction.reply('Pong!'); + } +}); + +client.login(process.env.TOKEN); diff --git a/02 - Registering Slash Commands/src/register-commands.js b/02 - Registering Slash Commands/src/register-commands.js new file mode 100644 index 0000000..4e3f71b --- /dev/null +++ b/02 - Registering Slash Commands/src/register-commands.js @@ -0,0 +1,33 @@ +require('dotenv').config(); +const { REST, Routes } = require('discord.js'); + +const commands = [ + { + name: 'hey', + description: 'Replies with hey!', + }, + { + name: 'ping', + description: 'Pong!', + }, +]; + +const rest = new REST({ version: '10' }).setToken(process.env.TOKEN); + +(async () => { + try { + console.log('Registering slash commands...'); + + await rest.put( + Routes.applicationGuildCommands( + process.env.CLIENT_ID, + process.env.GUILD_ID + ), + { body: commands } + ); + + console.log('Slash commands were registered successfully!'); + } catch (error) { + console.log(`There was an error: ${error}`); + } +})(); diff --git a/03 - Options and Choices/.env.example b/03 - Options and Choices/.env.example new file mode 100644 index 0000000..ac59ed5 --- /dev/null +++ b/03 - Options and Choices/.env.example @@ -0,0 +1,3 @@ +TOKEN = YOUR_BOT_TOKEN +CLIENT_ID = YOUR_BOT_ID +GUILD_ID = YOUR_SERVER_ID \ No newline at end of file diff --git a/03 - Options and Choices/package.json b/03 - Options and Choices/package.json new file mode 100644 index 0000000..6710c7b --- /dev/null +++ b/03 - Options and Choices/package.json @@ -0,0 +1,16 @@ +{ + "name": "discord-bot", + "version": "1.0.0", + "description": "", + "main": "src/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "discord.js": "^14.7.1", + "dotenv": "^16.0.3" + } +} diff --git a/03 - Options and Choices/src/index.js b/03 - Options and Choices/src/index.js new file mode 100644 index 0000000..33aa0b7 --- /dev/null +++ b/03 - Options and Choices/src/index.js @@ -0,0 +1,28 @@ +require('dotenv').config(); +const { Client, IntentsBitField } = require('discord.js'); + +const client = new Client({ + intents: [ + IntentsBitField.Flags.Guilds, + IntentsBitField.Flags.GuildMembers, + IntentsBitField.Flags.GuildMessages, + IntentsBitField.Flags.MessageContent, + ], +}); + +client.on('ready', (c) => { + console.log(`✅ ${c.user.tag} is online.`); +}); + +client.on('interactionCreate', (interaction) => { + if (!interaction.isChatInputCommand()) return; + + if (interaction.commandName === 'add') { + const num1 = interaction.options.get('first-number').value; + const num2 = interaction.options.get('second-number').value; + + interaction.reply(`The sum is ${num1 + num2}`); + } +}); + +client.login(process.env.TOKEN); diff --git a/03 - Options and Choices/src/register-commands.js b/03 - Options and Choices/src/register-commands.js new file mode 100644 index 0000000..4fdeda9 --- /dev/null +++ b/03 - Options and Choices/src/register-commands.js @@ -0,0 +1,57 @@ +require('dotenv').config(); +const { REST, Routes, ApplicationCommandOptionType } = require('discord.js'); + +const commands = [ + { + name: 'add', + description: 'Adds two numbers.', + options: [ + { + name: 'first-number', + description: 'The first number.', + type: ApplicationCommandOptionType.String, + choices: [ + { + name: 'one', + value: '1', + }, + { + name: 'two', + value: '2', + }, + { + name: 'three', + value: '3', + }, + ], + required: true, + }, + { + name: 'second-number', + description: 'The second number.', + type: ApplicationCommandOptionType.Number, + required: true, + }, + ], + }, +]; + +const rest = new REST({ version: '10' }).setToken(process.env.TOKEN); + +(async () => { + try { + console.log('Registering slash commands...'); + + await rest.put( + Routes.applicationGuildCommands( + process.env.CLIENT_ID, + process.env.GUILD_ID + ), + { body: commands } + ); + + console.log('Slash commands were registered successfully!'); + } catch (error) { + console.log(`There was an error: ${error}`); + } +})();