Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
railway-bot committed Sep 7, 2022
0 parents commit 6f20ad3
Show file tree
Hide file tree
Showing 8 changed files with 1,185 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .gitignore
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/
29 changes: 29 additions & 0 deletions README.md
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/#/).

[![Deploy on Railway](https://railway.app/button.svg)](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.
10 changes: 10 additions & 0 deletions Template.js
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!");
},
};
23 changes: 23 additions & 0 deletions package.json
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"
}
}
10 changes: 10 additions & 0 deletions src/Commands/hello.js
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! 🚅" });
},
};
10 changes: 10 additions & 0 deletions src/Commands/ping.js
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\` 🏓` });
},
};
60 changes: 60 additions & 0 deletions src/index.js
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.
Loading

0 comments on commit 6f20ad3

Please sign in to comment.