-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (70 loc) · 2 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
76
77
78
// require('dotenv').config();
const fs = require ("fs");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const { Client, Intents, Collection } = require ("discord.js");
const Discord = require('discord.js')
// const wait = require('node:timers/promises').setTimeout;
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
]
});
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);
}
// Runs when client connects to Discord.
client.on('ready', async () => {
console.log('Logged in as', client.user.tag)
const CLIENT_ID = client.user.id;
const rest = new REST({
version: "9"
}).setToken(process.env.DISCORD_TOKEN);
(async() => {
try{
if (process.env.ENV === "production"){
await rest.put(Routes.applicationCommands(CLIENT_ID), {
body: commands
});
console.log("Success fully registered commands globally.")
}
else{
await rest.put(Routes.applicationGuildCommands(CLIENT_ID,process.env.SERVER_ID), {
body: commands
});
console.log("Success fully registered commands locally.")
}
}
catch(err){
console.log(err);
}
})();
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try{
await command.execute(interaction);
}
catch(err){
console.log(err);
try{
await interaction.editReply("An error occured while executing that command. Please contact Dev");
}
catch(err){
console.log(err)
await interaction.editReply({
content: "An error occured while executing that command. Please contact a Dev",
emphemeral: true
});
}
}
});
client.login(process.env.DISCORD_TOKEN)