|
1 | 1 | import type { |
2 | 2 | ApplicationCommand, |
3 | 3 | ApplicationCommandDataResolvable, |
4 | | - Client, |
5 | 4 | Collection, |
6 | 5 | } from "discord.js"; |
7 | 6 | import { |
8 | 7 | SlashCommandBuilder, |
9 | 8 | type SlashCommandSubcommandBuilder, |
10 | 9 | } from "discord.js"; |
| 10 | +import type DjsClient from "../DjsClient"; |
11 | 11 | import type Command from "../interaction/Command"; |
12 | 12 | import type ContextMenu from "../interaction/ContextMenu"; |
13 | 13 | import type { Route } from "./CommandHandler"; |
14 | 14 |
|
15 | 15 | export default class ApplicationCommandHandler { |
16 | | - private readonly client: Client; |
| 16 | + private readonly client: DjsClient; |
17 | 17 | private commands: Route[] = []; |
18 | 18 | private contextMenus: ContextMenu[] = []; |
19 | 19 | private guilds: string[] = []; |
20 | 20 | private rootIdCache = new Map<string, Map<string, string>>(); |
| 21 | + private hasWarnedEmptyContext = false; |
21 | 22 |
|
22 | | - constructor(client: Client) { |
| 23 | + constructor(client: DjsClient) { |
23 | 24 | this.client = client; |
24 | 25 | } |
25 | 26 |
|
@@ -156,7 +157,12 @@ export default class ApplicationCommandHandler { |
156 | 157 | if (!cmd.name) { |
157 | 158 | cmd.setName(root); |
158 | 159 | } |
159 | | - return cmd.toJSON(); |
| 160 | + this.applyDefaultContext(cmd, routes); |
| 161 | + const json = cmd.toJSON(); |
| 162 | + if (json.contexts && json.contexts.length === 0) { |
| 163 | + delete json.contexts; |
| 164 | + } |
| 165 | + return json; |
160 | 166 | } |
161 | 167 |
|
162 | 168 | for (const [name, cmd] of subcommands) { |
@@ -191,7 +197,50 @@ export default class ApplicationCommandHandler { |
191 | 197 | }); |
192 | 198 | } |
193 | 199 |
|
194 | | - return builder.toJSON(); |
| 200 | + this.applyDefaultContext(builder, routes); |
| 201 | + const json = builder.toJSON(); |
| 202 | + if (json.contexts && json.contexts.length === 0) { |
| 203 | + delete json.contexts; |
| 204 | + } |
| 205 | + return json; |
| 206 | + } |
| 207 | + |
| 208 | + private applyDefaultContext( |
| 209 | + target: Command | SlashCommandBuilder, |
| 210 | + routes: Array<{ parts: string[]; cmd: Command }>, |
| 211 | + ): void { |
| 212 | + const defaultContext = this.client.getDjsConfig()?.commands?.defaultContext; |
| 213 | + if (!defaultContext) { |
| 214 | + return; |
| 215 | + } |
| 216 | + if (!Array.isArray(defaultContext) || defaultContext.length === 0) { |
| 217 | + if (!this.hasWarnedEmptyContext) { |
| 218 | + console.warn( |
| 219 | + "⚠️ config.commands.defaultContext is defined but empty. Default context will not be applied.", |
| 220 | + ); |
| 221 | + this.hasWarnedEmptyContext = true; |
| 222 | + } |
| 223 | + return; |
| 224 | + } |
| 225 | + |
| 226 | + try { |
| 227 | + const targetJson = target.toJSON(); |
| 228 | + if (targetJson.contexts && targetJson.contexts.length > 0) { |
| 229 | + return; |
| 230 | + } |
| 231 | + } catch {} |
| 232 | + |
| 233 | + for (const r of routes) { |
| 234 | + try { |
| 235 | + const cmdJson = r.cmd.toJSON(); |
| 236 | + if (cmdJson.contexts && cmdJson.contexts.length > 0) { |
| 237 | + target.setContexts(cmdJson.contexts); |
| 238 | + return; |
| 239 | + } |
| 240 | + } catch {} |
| 241 | + } |
| 242 | + |
| 243 | + target.setContexts(defaultContext); |
195 | 244 | } |
196 | 245 |
|
197 | 246 | private getRootDescription(root: string): string | undefined { |
|
0 commit comments