-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
94 lines (78 loc) · 3.24 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
import { readdirSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
import { Client, Collection, Events, GatewayIntentBits } from 'discord.js';
import config from './config.json' assert { type: 'json' };
const token = config.token;
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.cooldowns = new Collection();
client.commands = new Collection();
const commandFoldersPath = join(__dirname, 'commands');
const commandFolders = readdirSync(commandFoldersPath);
for (const folder of commandFolders) {
const commandsPath = join(commandFoldersPath, folder);
const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = join("file://", commandsPath, file);
const command = await import(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
client.buttons = new Collection();
const buttonsPath = join(__dirname, 'buttons');
const buttonFiles = readdirSync(buttonsPath);
for (const file of buttonFiles) {
const filePath = join("file://", buttonsPath, file);
const button = await import(filePath);
if ('data' in button && 'execute' in button) {
client.buttons.set(button.data.match, button);
} else {
console.log(`[WARNING] The button at ${filePath} is missing a required "data" or "execute" property.`);
}
}
client.once(Events.ClientReady, () => {
console.log('Ready!');
});
client.on(Events.InteractionCreate, async interaction => {
if (interaction.isChatInputCommand()) {
const command = client.commands.get(interaction.commandName);
if (!command) return;
const { cooldowns } = client;
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const defaultCooldownDuration = 3;
const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;
if (timestamps.has(interaction.user.id)) {
const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;
if (now < expirationTime) {
const expiredTimestamp = Math.round(expirationTime / 1000);
return interaction.reply({ content: `Please wait <t:${expiredTimestamp}:R> more second(s) before reusing the \`${command.name}\` command.`, ephemeral: true });
}
}
timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
} else if (interaction.isButton()) {
/**
* @type {Collection<String | RegExp, {data: {match: String | RegExp}, execute: Promise | Function}>}
*/
const buttons = client.buttons.filter((value, key) => interaction.customId.match(key) ? true : false);
buttons.forEach(async button => {
await button.execute(interaction, interaction.customId);
});
}
});
client.login(token);