-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
75 lines (61 loc) · 1.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const fs = require("fs");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v10");
const { Client, GatewayIntentBits, Collection } = require('discord.js');
// load env config
const dotenv = require('dotenv');
dotenv.config({path: './.env'});
// connect to db
const connectDB = require('./db-connection');
connectDB();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
]
});
const commandFiles = fs.readdirSync("./commands").filter(file => file.endsWith(".js"));
const commands = [];
client.commands = new Collection();
for(const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
client.commands.set(command.data.name, command);
}
client.once('ready', () => {
console.info(`Logged in as ${client.user.tag}!`);
const rest = new REST({
version: "10"
}).setToken(process.env.BOT_TOKEN);
(async () => {
try {
if(process.env.ENV === "production") {
await rest.put(Routes.applicationCommands(process.env.CLIENT_ID), {
body: commands
});
console.log("Successfully registered commands globally.")
} else {
await rest.put(Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID), {
body: commands
});
console.log("Successfully registered commands locally.")
}
} catch (err) {
if (err) console.error(err);
}
})();
});
client.on("interactionCreate", async interaction => {
if(!interaction.type === interaction.ApplicationCommand) return;
const command = client.commands.get(interaction.commandName);
if(!command) return;
try {
await command.execute(interaction);
} catch (err) {
if (err) console.log(err);
await interaction.reply({
content: "An error occured while executing that command.",
emphemeral: true
});
}
});
client.login(process.env.BOT_TOKEN);