This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
94 lines (75 loc) · 2.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
import config from './config.json' assert { type: 'json' };
import { Routes } from 'discord-api-types/v9';
import { REST } from '@discordjs/rest';
import fs from 'fs/promises';
import { ShardingManager } from 'discord.js';
if (!config.credentials.bot_token)
throw "Please provide a Discord bot token";
const CLIENT_ID = config.credentials.discord_client_id;
const TOKEN = config.credentials.bot_token;
const commands = [];
const commandFiles = (await fs.readdir('./commands')).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const { default: Command } = await import(`./commands/${file}`);
commands.push(new Command());
}
const rest = new REST({ version: '9' }).setToken(TOKEN);
const commandPayload = [];
for (const c of commands) {
const command = {
name: c.command,
description: c.description,
type: c?.type ?? 1,
defaultPermission: true
};
if (c.hasOwnProperty('permsRequired') && c.permsRequired.length > 0) {
command.defaultPermission = false;
}
if (c.hasOwnProperty('options')) {
command.options = c.options;
} else if (c?.argsRequired !== undefined && c.argsRequired > 0) {
command.options = [
{
name: 'query',
description: 'Query for the command.',
type: 3,
required: true
}
];
}
commandPayload.push(command);
}
const manager = new ShardingManager('./bot.js', { token: config.credentials.bot_token, mode: 'worker' });
manager.spawn();
manager.on('shardCreate', shard => {
shard.on('message', message => {
if (typeof message == 'string')
manager.broadcast(message)
});
console.log(`Launched shard ${shard.id}`);
});
try {
console.log('Started refreshing application (/) commands.');
const guildCommandPayload = [
...commandPayload,
{
name: 'reload',
description: 'Reload command files',
type: 1,
defaultPermission: true
}
];
if (config?.owner_guild != null) {
await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, config.owner_guild),
{ body: guildCommandPayload },
);
}
await rest.put(
Routes.applicationCommands(CLIENT_ID),
{ body: commandPayload },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}