-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.ts
61 lines (53 loc) · 2.36 KB
/
command.ts
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
import { REST } from '@discordjs/rest';
import { RESTPostAPIApplicationCommandsJSONBody, Routes } from 'discord-api-types/v9';
import { ApplicationCommandPermissionData, CacheType, CommandInteraction } from 'discord.js';
import { client, onReady } from './client';
import data = require('./discord.json');
const rest = new REST({ version: '9' }).setToken(data.token);
let commandJsons:RESTPostAPIApplicationCommandsJSONBody[]|null = null;
const commands = new Map<string, (interaction:CommandInteraction<CacheType>)=>Promise<void>|void>();
let commandPermissions:(ApplicationCommandPermissionData[]|null)[]|null = null;
export function registerCommand(
builder:{toJSON():RESTPostAPIApplicationCommandsJSONBody},
permissions:ApplicationCommandPermissionData[]|null,
cb:(interaction:CommandInteraction<CacheType>)=>Promise<void>|void):void {
const body = builder.toJSON();
commands.set(body.name, cb);
if (commandJsons === null) {
commandJsons = [body];
commandPermissions = [permissions];
process.nextTick(async()=>{
const cmdinfos = commandJsons!;
const cmdpermissions = commandPermissions!;
commandJsons = null;
commandPermissions = null;
for (const guildId of data.guilds) {
await rest.put(
Routes.applicationGuildCommands(data.applicationId, guildId),
{ body: cmdinfos }
);
}
onReady(async()=>{
for (const guildId of data.guilds) {
const guild = await client.guilds.fetch(guildId);
const commands = await guild.commands.fetch();
// let i=0;
// for (const [id, cmd] of commands) {
// const permissions = cmdpermissions[i++];
// if (permissions !== null) {
// cmd.permissions.add({ permissions }); // ERROR: Bots cannot use this endpoint
// }
// }
}
});
});
} else {
commandJsons.push(body);
commandPermissions!.push(permissions);
}
}
client.on('interactionCreate', async(interaction) => {
if (!interaction.isCommand()) return;
const cb = commands.get(interaction.commandName);
if (cb != null) await cb(interaction);
});