Update Discord JS w/ Slash Commands (#218)

* Changed up discord.js starter

I added prefixes to commands, and made it so it makes the content lowercase when checking for commands so if a user says "!pInG" it is still going to work

* New discord.js starter

Made the discord.js starter use slash commands and a handler!

* Update examples/discordjs/src/index.js

Co-authored-by: Milo <50248166+Milo123459@users.noreply.github.com>

* Update examples/discordjs/README.md

Co-authored-by: Milo <50248166+Milo123459@users.noreply.github.com>

* Made some new changes

* Update examples/discordjs/src/index.js

Co-authored-by: Milo <50248166+Milo123459@users.noreply.github.com>

* Changed indentation

* Update examples/discordjs/src/index.js

Co-authored-by: Milo <50248166+Milo123459@users.noreply.github.com>

* Update examples/discordjs/README.md

Co-authored-by: Milo <50248166+Milo123459@users.noreply.github.com>

* Formatted code with prettier

* Update examples/discordjs/src/index.js

Co-authored-by: Milo <50248166+Milo123459@users.noreply.github.com>

* Update examples/discordjs/src/index.js

Co-authored-by: Milo <50248166+Milo123459@users.noreply.github.com>

* Update README.md

* Update examples/discordjs/README.md

Co-authored-by: Milo <50248166+Milo123459@users.noreply.github.com>

Co-authored-by: Milo <50248166+Milo123459@users.noreply.github.com>
This commit is contained in:
Korab Arifi
2021-11-01 17:30:23 +01:00
committed by GitHub
parent 5dfd9de650
commit 38e1d00673
7 changed files with 345 additions and 203 deletions

View File

@@ -25,4 +25,6 @@ This example starts a Discord bot using [discord.js](https://discord.js.org/#/).
## 📝 Notes
The server started launches a Discord bot with a couple of basic commands. The code is located at `src/index.js`.
- Upon the first run of the starter, it can take up to an hour for your slash commands to be visible in the Discord Client.
- To create a new command, just create a file in the `Commands` directory. You can take a look at the `Template.js` file for an example of what commands should look like. For any additional help see the [discord.js guide](https://discordjs.guide).
- If you need any additional help with this, join our [Discord server](https://discord.gg/railway) and create a thread in the project help channel.

View File

@@ -0,0 +1,10 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
module.exports = {
data: new SlashCommandBuilder()
.setName("name")
.setDescription("My cool command does this!"),
execute: async (interaction, client) => {
return interaction.reply("Hey! you used my command!");
},
};

View File

@@ -9,7 +9,10 @@
"start": "node ."
},
"dependencies": {
"discord.js": "^13.1.0"
"@discordjs/builders": "^0.8.1",
"@discordjs/rest": "^0.1.0-canary.0",
"discord-api-types": "^0.24.0",
"discord.js": "^13.3.0"
},
"devDependencies": {
"nodemon": "^2.0.12"

View File

@@ -0,0 +1,10 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
module.exports = {
data: new SlashCommandBuilder()
.setName("hello")
.setDescription("Say Hello To me!"),
execute: async (interaction, client) => {
return interaction.reply("Choo choo! 🚅");
},
};

View File

@@ -0,0 +1,10 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
module.exports = {
data: new SlashCommandBuilder()
.setName("ping")
.setDescription("Get the bots latency!"),
execute: async (interaction, client) => {
return interaction.reply(`Pong \`${client.ws.ping}ms\` 🏓`);
},
};

View File

@@ -1,21 +1,60 @@
const { Client, Intents } = require("discord.js");
const { REST } = require("@discordjs/rest"); // Define REST.
const { Routes } = require("discord-api-types/v9"); // Define Routes.
const fs = require("fs"); // Define fs (file system).
const { Client, Intents, Collection } = require("discord.js"); // Define Client, Intents, and Collection.
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
});
// Token from Railway
const TOKEN = process.env.DISCORD_TOKEN;
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
}); // Connect to our discord bot.
const commands = new Collection(); // Where the bot (slash) commands will be stored.
const commandarray = []; // Array to store commands for sending to the REST API.
const token = process.env.DISCORD_TOKEN; // Token from Railway Env Variable.
// Execute code when the "ready" client event is triggered.
client.once("ready", () => {
const commandFiles = fs
.readdirSync("src/Commands")
.filter(file => file.endsWith(".js")); // Get and filter all the files in the "Commands" Folder.
// Loop through the command files
for (const file of commandFiles) {
const command = require(`./Commands/${file}`); // Get and define the command file.
commands.set(command.data.name, command); // Set the command name and file for handler to use.
commandarray.push(command.data.toJSON()); // Push the command data to an array (for sending to the API).
}
const rest = new REST({ version: "9" }).setToken(token); // Define "rest" for use in registering commands
// Register slash commands.
;(async () => {
try {
console.log("Started refreshing application (/) commands.");
await rest.put(Routes.applicationCommands(client.user.id), {
body: commandarray,
});
console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
})();
console.log(`Logged in as ${client.user.tag}!`);
});
// Command handler.
client.on("interactionCreate", async interaction => {
if (!interaction.isCommand()) return;
client.on("message", msg => {
if (msg.content === "ping") {
msg.reply("Pong 🏓");
} else if (msg.content === "hello") {
msg.reply("Choo choo! 🚅");
const command = commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, client);
} catch (error) {
console.error(error);
return interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});
client.login(TOKEN);
client.login(token); // Login to the bot client via the defined "token" string.

File diff suppressed because it is too large Load Diff