@@ -14,6 +14,8 @@ import * as util from "util";
1414import { M } from "../utils/debugging-and-logging.js" ;
1515import { Wheatley } from "../wheatley.js" ;
1616
17+ const global_context_menu_limit = 5 ;
18+
1719export class CommandSetBuilder {
1820 commands : ( Discord . SlashCommandBuilder | Discord . ContextMenuCommandBuilder ) [ ] = [ ] ;
1921 text_commands : Record < string , BotTextBasedCommand < unknown [ ] > > = { } ;
@@ -83,17 +85,57 @@ export class CommandSetBuilder {
8385 async finalize ( token : string ) {
8486 try {
8587 const rest = new REST ( { version : "10" } ) . setToken ( token ) ;
86- const route = this . wheatley . freestanding
87- ? Discord . Routes . applicationGuildCommands ( this . wheatley . user . id , this . wheatley . guild . id )
88- : Discord . Routes . applicationCommands ( this . wheatley . user . id ) ;
89-
90- this . wheatley . info ( `Registering ${ this . commands . length } application commands` ) ;
91- M . log (
92- "Sending application commands:" ,
93- this . commands . length ,
94- this . commands . map ( builder => builder . name ) ,
95- ) ;
96- await rest . put ( route , { body : this . commands } ) ;
88+ if ( this . wheatley . freestanding ) {
89+ const route = Discord . Routes . applicationGuildCommands ( this . wheatley . user . id , this . wheatley . guild . id ) ;
90+ this . wheatley . info ( `Registering ${ this . commands . length } application commands to guild` ) ;
91+ M . log (
92+ "Sending application commands to guild:" ,
93+ this . commands . length ,
94+ this . commands . map ( builder => builder . name ) ,
95+ ) ;
96+ await rest . put ( route , { body : this . commands } ) ;
97+ } else {
98+ const slash_commands = this . commands . filter ( cmd => cmd instanceof Discord . SlashCommandBuilder ) ;
99+ const context_menu_commands = this . commands . filter (
100+ cmd => cmd instanceof Discord . ContextMenuCommandBuilder ,
101+ ) ;
102+
103+ const global_context_menu_commands = context_menu_commands . slice ( 0 , global_context_menu_limit ) ;
104+ const guild_context_menu_commands = context_menu_commands . slice ( global_context_menu_limit ) ;
105+
106+ const global_commands = [ ...slash_commands , ...global_context_menu_commands ] ;
107+
108+ this . wheatley . info (
109+ `Registering ${ global_commands . length } commands globally ` +
110+ `(${ slash_commands . length } slash, ${ global_context_menu_commands . length } context menu) and ` +
111+ `${ guild_context_menu_commands . length } context menu commands to guild` ,
112+ ) ;
113+
114+ if ( global_commands . length > 0 ) {
115+ M . log (
116+ "Sending commands globally:" ,
117+ global_commands . length ,
118+ global_commands . map ( builder => builder . name ) ,
119+ ) ;
120+ await rest . put ( Discord . Routes . applicationCommands ( this . wheatley . user . id ) , {
121+ body : global_commands ,
122+ } ) ;
123+ }
124+
125+ if ( guild_context_menu_commands . length > 0 ) {
126+ M . log (
127+ "Sending context menu commands to guild:" ,
128+ guild_context_menu_commands . length ,
129+ guild_context_menu_commands . map ( builder => builder . name ) ,
130+ ) ;
131+ await rest . put (
132+ Discord . Routes . applicationGuildCommands ( this . wheatley . user . id , this . wheatley . guild . id ) ,
133+ {
134+ body : guild_context_menu_commands ,
135+ } ,
136+ ) ;
137+ }
138+ }
97139 M . log ( "Finished sending commands" ) ;
98140 return {
99141 text_commands : this . text_commands ,
0 commit comments