Skip to content

Commit 6e00a57

Browse files
authored
Merge branch 'main' into IslandRhythms/findByVehicle
2 parents 0c227d2 + 9115aa6 commit 6e00a57

24 files changed

Lines changed: 223 additions & 6545 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,6 @@ dist
105105
# Lockfiles
106106
package-lock.json
107107
yarn.lock
108+
109+
# config
110+
config.json

discord-bot/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Steps to run Bot:
2+
1. Create a config.json with the keys from example.json and the values from the developer portal and your discord server.*
3+
2. run npm install
4+
3. run node ./deploy-commands.js
5+
4. run node ./index.js
6+
7+
* `guildId` is obtained by going into your server's settings and clicking on the widget tab. The server id is the `guildId`.
8+
* `clientId` is obtained by going to the discord developer portal, clicking your bot, and then clicking OAuth2.
9+
* `token` is obtained by going to the discord developer portal, clicking your bot, and then clicking on Bot.
10+
There will be a button to reset the token and this is by design. Discord will only show the token once so when you hit reset, copy it to a secure place.
11+
* Make sure to enable slash commands when selecting bot permissions.
12+
13+
For more information on setting up your bot and adding it to your server: https://discordjs.guide/preparations/setting-up-a-bot-application.html
14+
15+
You should now be able to run the discord bot in your server locally.
16+
17+
Anytime you add or update commmands in the command folder, run step 3 again.
18+
19+
All replies must be strings.
20+
21+
Built on
22+
23+
Node: 16.13.2
24+
Mongoose: 6.6.1
25+
Discordjs: 14.3.0
26+
@discordjs/rest: 1.1.0
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { SlashCommandBuilder } = require('discord.js');
2+
const Bot = require('../models/bot')
3+
4+
module.exports = {
5+
data: new SlashCommandBuilder()
6+
.setName('count')
7+
.setDescription('counts documents in the database'),
8+
async execute(interaction) {
9+
const num = await Bot.countDocuments();
10+
console.log(num);
11+
await interaction.reply(num.toString());
12+
},
13+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { SlashCommandBuilder } = require('discord.js');
2+
const Bot = require('../models/bot')
3+
4+
module.exports = {
5+
data: new SlashCommandBuilder()
6+
.setName('createdocument')
7+
.setDescription('creates a document'),
8+
async execute(interaction) {
9+
console.log('hello');
10+
await Bot.create({ name: 'I am a document'});
11+
await interaction.reply('done!');
12+
},
13+
};

discord-bot/commands/ping.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { SlashCommandBuilder } = require('discord.js');
2+
3+
module.exports = {
4+
data: new SlashCommandBuilder()
5+
.setName('ping')
6+
.setDescription('Replies with Pong!'),
7+
async execute(interaction) {
8+
await interaction.reply('Pong!');
9+
},
10+
};

discord-bot/deploy-commands.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { Routes } = require('discord.js');
2+
const { REST } = require('@discordjs/rest');
3+
const { clientId, guildId, token } = require('./config.json');
4+
const fs = require('node:fs');
5+
const path = require('node:path');
6+
7+
const commands = [];
8+
9+
const commandsPath = path.join(__dirname, 'commands');
10+
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
11+
12+
for (const file of commandFiles) {
13+
const filePath = path.join(commandsPath, file);
14+
console.log(filePath);
15+
const command = require(filePath);
16+
console.log(command);
17+
commands.push(command.data.toJSON());
18+
}
19+
20+
const rest = new REST({ version: '10' }).setToken(token);
21+
22+
rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
23+
.then((data) => console.log(`Successfully registered ${data.length} application commands.`))
24+
.catch(console.error);

discord-bot/example.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"token": "",
3+
"clientId": "",
4+
"guildId": ""
5+
}

discord-bot/index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Require the necessary discord.js classes
2+
const { Client, Collection, GatewayIntentBits } = require('discord.js');
3+
const { token } = require('./config.json');
4+
const fs = require('fs');
5+
const path = require('node:path');
6+
const mongoose = require('mongoose');
7+
8+
// Create a new client instance
9+
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
10+
11+
12+
client.commands = new Collection();
13+
const commandsPath = path.join(__dirname, 'commands');
14+
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
15+
16+
for (const file of commandFiles) {
17+
const filePath = path.join(commandsPath, file);
18+
const command = require(filePath);
19+
// Set a new item in the Collection
20+
// With the key as the command name and the value as the exported module
21+
client.commands.set(command.data.name, command);
22+
}
23+
24+
25+
// When the client is ready, run this code (only once)
26+
client.once('ready', async () => {
27+
console.log('Ready!');
28+
});
29+
30+
client.on('interactionCreate', async interaction => {
31+
if (!interaction.isChatInputCommand()) return;
32+
33+
const command = client.commands.get(interaction.commandName);
34+
35+
if (!command) return;
36+
37+
try {
38+
await command.execute(interaction);
39+
} catch (error) {
40+
console.error(error);
41+
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
42+
}
43+
});
44+
45+
run();
46+
47+
async function run() {
48+
// connect to mongoose
49+
await mongoose.connect('mongodb://localhost:27017');
50+
// Login to Discord with your client's token
51+
client.login(token);
52+
}

discord-bot/models/bot.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const mongoose = require('mongoose');
2+
3+
const botSchema = new mongoose.Schema({
4+
name: String
5+
});
6+
7+
const Bot = mongoose.model('Bot', botSchema);
8+
9+
module.exports = Bot;

discord-bot/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "discord-bot",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node ./index.js"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"@discordjs/rest": "^1.1.0",
14+
"@sapphire/framework": "^3.1.1",
15+
"discord.js": "^14.3.0",
16+
"mongoose": "^6.6.1"
17+
}
18+
}

0 commit comments

Comments
 (0)