-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (104 loc) · 3.62 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require("node:fs");
const process = require("node:process");
const logger = require("js-logger");
const { Client, Collection, Intents } = require("discord.js");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const { config } = require("./config");
require("dotenv").config();
const TOKEN = process.env.DISCORD_TOKEN;
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.DIRECT_MESSAGES
],
partials: ["MESSAGE", "CHANNEL", "REACTION"]
});
async function main() {
process.on("exit", () => {
client.emit("shutdown");
logger.info("Graceful shutdown completed. Exiting...");
logger.info("Process terminated");
});
process.on("SIGINT", () => {
logger.info("Caught interrupt signal");
process.exit();
});
logger.useDefaults({
defaultLevel: logger.DEBUG,
formatter: (messages, context) =>
messages.unshift(`[${new Date().toUTCString()}] [${context.level.name}]: `)
});
module.exports = { logger };
client.once("ready", () => {
registerCommands();
handleCommands();
logger.info("Bot loaded!");
});
client.on("messageCreate", message => {
if(message.channelId === config.discord.verificationChannelId && message.type != "APPLICATION_COMMAND")
message.delete();
});
client.login(TOKEN);
}
/**
* Load all command files from the "commands" folder, and POST them to the Discord
* command endpoint for the specific server.
*
* @private
*
*/
function registerCommands() {
logger.info("Loading commands!");
client.commands = new Collection();
const files = fs.readdirSync("./src/commands")
.filter(file => file.endsWith(".js") && file != "example.js");
for (const file of files) {
const command = require(`./commands/${file}`);
if (!command.enabled)
continue;
client.commands.set(command.data.name, command);
logger.info(`Loaded command from file: commands/${file}`);
}
const rest = new REST({ version: "9" }).setToken(TOKEN);
(async () => {
try {
logger.info("Started refreshing application (/) commands.");
await rest.put(
Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID),
{ body: client.commands.map(command => command.data.toJSON()) },
);
logger.info("Successfully reloaded application (/) commands.");
} catch (error) {
logger.error(error);
}
})();
}
function handleCommands() {
client.on("interactionCreate", async interaction => {
if (!interaction.isCommand())
return;
const command = client.commands.get(interaction.commandName);
if (!command)
return;
// const roleRequired = command.roleRequired;
// if(interaction.member.roles.cache.has(roleRequired)) {
try {
await command.execute(interaction);
} catch (error) {
logger.error(error);
await interaction.reply({ content: "There was an error while executing this command!", ephemeral: true });
}
// } else
// await interaction.reply({
// embeds: [NoPermissionEmbed],
// ephemeral: true,
// });
});
}
main();