-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6f20ad3
Showing
8 changed files
with
1,185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
# out dir | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
title: Discord bot | ||
description: A Discord bot written in JavaScript | ||
tags: | ||
- discord.js | ||
- javascript | ||
--- | ||
|
||
# Discord.js Example | ||
|
||
This example starts a Discord bot using [discord.js](https://discord.js.org/#/). | ||
|
||
[](https://railway.app/new?template=https%3A%2F%2Fgithub.com%2Frailwayapp%2Fexamples%2Ftree%2Fmaster%2Fexamples%2Fdiscordjs&envs=DISCORD_TOKEN&DISCORD_TOKENDesc=Token+of+the+Discord+account+used) | ||
|
||
## ✨ Features | ||
|
||
- Discord.js | ||
- JavaScript | ||
|
||
## 💁♀️ How to use | ||
|
||
- Install dependencies `yarn` | ||
- Connect to your Railway project `railway link` | ||
- Start the bot `railway run yarn dev` | ||
|
||
## 📝 Notes | ||
|
||
- 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!"); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "discordjs", | ||
"version": "1.0.0", | ||
"main": "src/index.js", | ||
"author": "Jake Runzer", | ||
"license": "MIT", | ||
"scripts": { | ||
"dev": "nodemon .", | ||
"start": "node ." | ||
}, | ||
"dependencies": { | ||
"@discordjs/builders": "^0.12.0", | ||
"@discordjs/rest": "^0.1.0-canary.0", | ||
"discord-api-types": "^0.24.0", | ||
"discord.js": "^13.6.0" | ||
}, | ||
"devDependencies": { | ||
"nodemon": "^2.0.12" | ||
}, | ||
"engines": { | ||
"node": ">=16.6.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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({ content: "Choo choo! 🚅" }); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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({ content: `Pong \`${client.ws.ping}ms\` 🏓` }); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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], | ||
}); // 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; | ||
|
||
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); // Login to the bot client via the defined "token" string. |
Oops, something went wrong.