-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (109 loc) · 3.45 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
128
129
130
131
132
133
/* eslint-disable no-param-reassign */
/* eslint-disable no-shadow */
/* eslint-disable import/no-dynamic-require */
// env config
require("dotenv").config({ path: "./.env" });
const path = require("node:path");
const fs = require("node:fs");
const {
Client,
Collection,
Events,
GatewayIntentBits,
REST,
Routes,
Collector,
MessageCollector,
} = require("discord.js");
// environmental configuation
const ENV = {
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
botToken: process.env.BOT_TOKEN,
botScopes: process.env.BOT_PERMISSION_SCOPES,
guildID: process.env.GUILD_ID,
};
// setup
const client = new Client({
intents: GatewayIntentBits.Guilds,
});
// Add slash command handlers to client
// eslint-disable-next-line no-use-before-define
addCommandsToClient(client);
// Register slack commands to server using REST api
// eslint-disable-next-line no-use-before-define
registerCommands();
console.log(client.commands);
// handlers
client.once(Events.ClientReady, (c) => {
console.log(`Client Ready`);
console.log(`Logged in as ${c.user.tag}`);
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isButton()) return;
const buttonID = interaction.component.customId;
console.log(`User: ${interaction.user.id} \n Pressed: ${buttonID}`);
await interaction.message.react("🗿");
await interaction.reply({ content: "Vote Submitted", ephemeral: true });
});
// start up
client.login(ENV.botToken).catch((err) => console.error(err));
// utils
// TODO: These are gross and they need to be abstracted out
function addCommandsToClient(client) {
client.commands = new Collection();
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
}
}
function registerCommands() {
const commands = [];
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
console.log(command.data.toJSON());
commands.push(command.data.toJSON());
}
const rest = new REST({ version: "10" }).setToken(ENV.botToken);
(async () => {
try {
console.log(
`Started refreshing ${commands.length} application (/) commands.`
);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(ENV.clientID, ENV.guildID),
{ body: commands }
);
console.log(
`Successfully reloaded ${data.length} application (/) commands.`
);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error("FAILED TO REGISTER/RELOAD SLASH COMMANDS WITH GUILD");
console.error(error);
}
})();
}