This repository was archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
58 lines (45 loc) · 2.41 KB
/
client.js
File metadata and controls
58 lines (45 loc) · 2.41 KB
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
import Discord from 'discord.js';
import logger from './utils/logger.js';
import dropCommand from './commands/drop.js';
import squeakCommand from './commands/squeak.js';
import pingCommand from './commands/ping.js';
import helpCommand from './commands/help.js';
export default function () {
global.commands = new Discord.Collection();
commands.set(dropCommand.data.name, dropCommand);
commands.set(squeakCommand.data.name, squeakCommand);
commands.set(pingCommand.data.name, pingCommand);
commands.set(helpCommand.data.name, helpCommand);
const client = new Discord.Client({ intents: [Discord.GatewayIntentBits.Guilds] });
client.on('ready', () => {
logger.info(`Logged in as ${client.user.tag}`);
if (process.argv.includes('--register-commands')) {
const rest = new Discord.REST().setToken(process.env.BOT_TOKEN);
logger.info('Registering slash commands');
rest.put(Discord.Routes.applicationCommands(client.user.id), { body: commands.map(command => command.data) }).then(() => {
logger.info('Successfully registered slash commands');
}).catch(err => logger.error(err));
}
});
const cooldowns = new Discord.Collection();
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
if (!commands.has(interaction.commandName)) return;
const command = commands.get(interaction.commandName);
logger.info(`Received command ${command.data.name} from ${interaction.user.tag} (${interaction.client.ws.ping} ms)`);
if (command.cooldown) {
if (!cooldowns.get(command.data.name)) cooldowns.set(command.data.name, new Discord.Collection());
if ((cooldowns.get(command.data.name).get(interaction.user.id) || Date.now()) > Date.now()) return interaction.reply({ content: `You're on cooldown! Please wait **${((cooldowns.get(command.data.name).get(interaction.user.id) - Date.now()) / 1000).toFixed(2)}** seconds.`, ephemeral: true });
cooldowns.get(command.data.name).set(interaction.user.id, Date.now() + (command.cooldown * 1000));
};
try {
return command.execute(interaction);
} catch (error) {
logger.error('There was an error executing the command');
logger.error(error.stack);
return interaction.reply({ content: 'There was an error executing the command.', ephemeral: true });
};
});
client.login(process.env.BOT_TOKEN).catch(err => logger.error(err));
global.client = client;
}