6
6
import type {
7
7
ButtonInteraction ,
8
8
CommandInteraction ,
9
- ContextMenuInteraction ,
10
9
Interaction ,
10
+ MessageContextMenuInteraction ,
11
+ UserContextMenuInteraction ,
11
12
} from "discord.js" ;
12
13
import path from "path" ;
13
14
import type Makibot from "../Makibot" ;
@@ -49,8 +50,23 @@ export interface CommandInteractionHandler extends BaseInteractionHandler {
49
50
| Omit < SlashCommandBuilder , "addSubcommand" | "addSubcommandGroup" > ;
50
51
}
51
52
52
- export interface ContextMenuInteractionHandler extends BaseInteractionHandler {
53
- handle ( event : ContextMenuInteraction ) : Promise < void > ;
53
+ export interface UserContextMenuInteractionHandler extends BaseInteractionHandler {
54
+ /** Handle the command when sent to a guild. */
55
+ handleGuild ?: ( event : UserContextMenuInteraction ) => Promise < void > ;
56
+
57
+ /** Handle the command when sent to a DM. */
58
+ handleDM ?: ( event : UserContextMenuInteraction ) => Promise < void > ;
59
+
60
+ build ( ) : ContextMenuCommandBuilder ;
61
+ }
62
+
63
+ export interface MessageContextMenuInteractionHandler extends BaseInteractionHandler {
64
+ /** Handle the command when sent to a guild. */
65
+ handleGuild ?: ( event : MessageContextMenuInteraction ) => Promise < void > ;
66
+
67
+ /** Handle the command when sent to a DM. */
68
+ handleDM ?: ( event : MessageContextMenuInteraction ) => Promise < void > ;
69
+
54
70
build ( ) : ContextMenuCommandBuilder ;
55
71
}
56
72
@@ -75,12 +91,14 @@ function loadInteractions<T extends BaseInteractionHandler>(path: string): { [k:
75
91
76
92
export class InteractionManager {
77
93
private commands : Index < CommandInteractionHandler > ;
78
- private menus : Index < ContextMenuInteractionHandler > ;
94
+ private usermenus : Index < UserContextMenuInteractionHandler > ;
95
+ private messagemenus : Index < MessageContextMenuInteractionHandler > ;
79
96
private buttons : Index < ButtonInteractionHandler > ;
80
97
81
98
constructor ( readonly root : string , private readonly client : Makibot ) {
82
99
this . commands = loadInteractions ( path . join ( root , "commands" ) ) ;
83
- this . menus = loadInteractions ( path . join ( root , "menus" ) ) ;
100
+ this . usermenus = loadInteractions ( path . join ( root , "usermenus" ) ) ;
101
+ this . messagemenus = loadInteractions ( path . join ( root , "messagemenus" ) ) ;
84
102
this . buttons = loadInteractions ( path . join ( root , "buttons" ) ) ;
85
103
client . on ( "interactionCreate" , this . handleInteraction . bind ( this ) ) ;
86
104
}
@@ -90,7 +108,11 @@ export class InteractionManager {
90
108
await this . handleCommandInteraction ( interaction ) ;
91
109
}
92
110
if ( interaction . isContextMenu ( ) ) {
93
- await this . handleContextMenuInteraction ( interaction ) ;
111
+ if ( interaction . isUserContextMenu ( ) ) {
112
+ await this . handleUserContextMenuInteraction ( interaction ) ;
113
+ } else if ( interaction . isMessageContextMenu ( ) ) {
114
+ await this . handleMessageContextMenuInteraction ( interaction ) ;
115
+ }
94
116
}
95
117
if ( interaction . isButton ( ) ) {
96
118
await this . handleButtonInteraction ( interaction ) ;
@@ -132,10 +154,77 @@ export class InteractionManager {
132
154
}
133
155
}
134
156
135
- private async handleContextMenuInteraction ( interaction : ContextMenuInteraction ) : Promise < void > {
136
- const handler = this . menus [ interaction . commandName ] ;
157
+ private async handleUserContextMenuInteraction (
158
+ interaction : UserContextMenuInteraction
159
+ ) : Promise < void > {
160
+ const handler = this . usermenus [ interaction . commandName ] ;
161
+ if ( handler ) {
162
+ if ( interaction . inGuild ( ) ) {
163
+ if ( handler . handleGuild ) {
164
+ await handler . handleGuild ( interaction ) ;
165
+ } else {
166
+ const toast = createToast ( {
167
+ title : "Menú no apto en una guild" ,
168
+ description : "Este menú no se puede pulsar en una guild" ,
169
+ severity : "error" ,
170
+ } ) ;
171
+ await interaction . reply ( {
172
+ embeds : [ toast ] ,
173
+ ephemeral : true ,
174
+ } ) ;
175
+ }
176
+ } else {
177
+ if ( handler . handleDM ) {
178
+ await handler . handleDM ( interaction ) ;
179
+ } else {
180
+ const toast = createToast ( {
181
+ title : "Menú no apto fuera de una guild" ,
182
+ description : "Este menú no se puede pulsar fuera de una guild" ,
183
+ severity : "error" ,
184
+ } ) ;
185
+ await interaction . reply ( {
186
+ embeds : [ toast ] ,
187
+ ephemeral : true ,
188
+ } ) ;
189
+ }
190
+ }
191
+ }
192
+ }
193
+
194
+ private async handleMessageContextMenuInteraction (
195
+ interaction : MessageContextMenuInteraction
196
+ ) : Promise < void > {
197
+ const handler = this . messagemenus [ interaction . commandName ] ;
137
198
if ( handler ) {
138
- await handler . handle ( interaction ) ;
199
+ if ( interaction . inGuild ( ) ) {
200
+ if ( handler . handleGuild ) {
201
+ await handler . handleGuild ( interaction ) ;
202
+ } else {
203
+ const toast = createToast ( {
204
+ title : "Menú no apto en una guild" ,
205
+ description : "Este menú no se puede pulsar en una guild" ,
206
+ severity : "error" ,
207
+ } ) ;
208
+ await interaction . reply ( {
209
+ embeds : [ toast ] ,
210
+ ephemeral : true ,
211
+ } ) ;
212
+ }
213
+ } else {
214
+ if ( handler . handleDM ) {
215
+ await handler . handleDM ( interaction ) ;
216
+ } else {
217
+ const toast = createToast ( {
218
+ title : "Menú no apto fuera de una guild" ,
219
+ description : "Este menú no se puede pulsar fuera de una guild" ,
220
+ severity : "error" ,
221
+ } ) ;
222
+ await interaction . reply ( {
223
+ embeds : [ toast ] ,
224
+ ephemeral : true ,
225
+ } ) ;
226
+ }
227
+ }
139
228
}
140
229
}
141
230
0 commit comments