Skip to content

Commit

Permalink
Initial commit - Part 1, Part 2, Part 3
Browse files Browse the repository at this point in the history
  • Loading branch information
notunderctrl committed Dec 21, 2022
0 parents commit 18efa80
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions 01 - Setting up project/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TOKEN = YOUR_BOT_TOKEN
16 changes: 16 additions & 0 deletions 01 - Setting up project/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
27 changes: 27 additions & 0 deletions 01 - Setting up project/src/index.js
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 3 additions & 0 deletions 02 - Registering Slash Commands/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TOKEN = YOUR_BOT_TOKEN
CLIENT_ID = YOUR_BOT_ID
GUILD_ID = YOUR_SERVER_ID
16 changes: 16 additions & 0 deletions 02 - Registering Slash Commands/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
29 changes: 29 additions & 0 deletions 02 - Registering Slash Commands/src/index.js
Original file line number Diff line number Diff line change
@@ -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);
33 changes: 33 additions & 0 deletions 02 - Registering Slash Commands/src/register-commands.js
Original file line number Diff line number Diff line change
@@ -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}`);
}
})();
3 changes: 3 additions & 0 deletions 03 - Options and Choices/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TOKEN = YOUR_BOT_TOKEN
CLIENT_ID = YOUR_BOT_ID
GUILD_ID = YOUR_SERVER_ID
16 changes: 16 additions & 0 deletions 03 - Options and Choices/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
28 changes: 28 additions & 0 deletions 03 - Options and Choices/src/index.js
Original file line number Diff line number Diff line change
@@ -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);
57 changes: 57 additions & 0 deletions 03 - Options and Choices/src/register-commands.js
Original file line number Diff line number Diff line change
@@ -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}`);
}
})();

0 comments on commit 18efa80

Please sign in to comment.