1
1
/* eslint-disable no-process-exit */
2
2
/* eslint-disable unicorn/no-process-exit */
3
3
4
+ import { SlashCommandBuilder } from '@discordjs/builders' ;
4
5
import { REST } from '@discordjs/rest' ;
5
- import { Routes } from 'discord-api-types/v9' ;
6
+ import { APIApplicationCommand , APIGuild , APIPartialGuild , Routes } from 'discord-api-types/v9' ;
6
7
7
8
import commands from '../commands' ;
8
9
import config from '../config' ;
@@ -14,17 +15,78 @@ if (!clientID) {
14
15
process . exit ( 1 ) ;
15
16
}
16
17
18
+ class API {
19
+ private rest : REST ;
20
+
21
+ constructor ( token : string , private clientID : string , private guildID ?: string ) {
22
+ this . rest = new REST ( { version : '9' } ) . setToken ( token ) ;
23
+ }
24
+
25
+ async list ( ) : Promise < APIApplicationCommand [ ] > {
26
+ const route = this . guildID
27
+ ? Routes . applicationGuildCommands ( this . clientID , this . guildID )
28
+ : Routes . applicationCommands ( this . clientID ) ;
29
+
30
+ const response = await this . rest . get ( route ) ;
31
+ return response as APIApplicationCommand [ ] ;
32
+ }
33
+
34
+ async install ( commands : SlashCommandBuilder [ ] ) : Promise < APIApplicationCommand [ ] > {
35
+ const route = this . guildID
36
+ ? Routes . applicationGuildCommands ( this . clientID , this . guildID )
37
+ : Routes . applicationCommands ( this . clientID ) ;
38
+ const body = commands . map ( ( command ) => command . toJSON ( ) ) ;
39
+
40
+ const response = await this . rest . put ( route , { body } ) ;
41
+ return response as APIApplicationCommand [ ] ;
42
+ }
43
+
44
+ async uninstall ( command : APIApplicationCommand ) : Promise < void > {
45
+ const route = command . guild_id
46
+ ? Routes . applicationGuildCommand ( this . clientID , command . guild_id , command . id )
47
+ : Routes . applicationCommand ( this . clientID , command . id ) ;
48
+
49
+ await this . rest . delete ( route ) ;
50
+ }
51
+
52
+ async permission ( command : string , guildID : string , user : string , permission = true ) : Promise < void > {
53
+ const route = Routes . applicationCommandPermissions ( this . clientID , guildID , command ) ;
54
+ const body = { permissions : [ { id : user , type : 2 , permission } ] } ;
55
+
56
+ await this . rest . put ( route , { body } ) ;
57
+ }
58
+
59
+ async guilds ( ) : Promise < string [ ] > {
60
+ const response = await this . rest . get ( Routes . userGuilds ( ) ) ;
61
+ const guilds = response as APIPartialGuild [ ] ;
62
+ return guilds . map ( ( guild ) => guild . id ) ;
63
+ }
64
+
65
+ async guild ( guildID : string ) : Promise < APIGuild > {
66
+ const response = await this . rest . get ( Routes . guild ( guildID ) ) ;
67
+ return response as APIGuild ;
68
+ }
69
+ }
70
+
17
71
( async ( ) => {
18
- const rest = new REST ( { version : '9' } ) . setToken ( config . token ) ;
19
- const route = ! config . guildID
20
- ? Routes . applicationCommands ( clientID )
21
- : Routes . applicationGuildCommands ( clientID , config . guildID ) ;
72
+ const api = new API ( config . token , clientID , config . guildID ) ;
22
73
23
74
try {
75
+ if ( process . argv . includes ( '--uninstall' ) ) {
76
+ for ( const command of await api . list ( ) ) {
77
+ logger . info (
78
+ { id : command . id , command : command . name , guild : command . guild_id } ,
79
+ 'Deleting application (/) command' ,
80
+ ) ;
81
+ await api . uninstall ( command ) ;
82
+ }
83
+ return ;
84
+ }
85
+
24
86
logger . info ( { guild : config . guildID , commands : [ ...commands . keys ( ) ] } , 'Installing application (/) commands' ) ;
25
- await rest . put ( route , { body : commands . map ( ( command ) => command . config . toJSON ( ) ) } ) ;
87
+ await api . install ( commands . map ( ( command ) => command . config ) ) ;
26
88
} catch ( error ) {
27
- logger . fatal ( error as Error ) ;
89
+ logger . fatal ( error ) ;
28
90
process . exit ( 1 ) ;
29
91
}
30
92
} ) ( ) ;
0 commit comments