|
| 1 | +import { ParsedCommand, ParsedMiddleware } from '@commandkit/router'; |
| 2 | +import type { CommandKit } from '../../CommandKit'; |
| 3 | +import { |
| 4 | + Awaitable, |
| 5 | + ContextMenuCommandBuilder, |
| 6 | + Locale, |
| 7 | + SlashCommandBuilder, |
| 8 | +} from 'discord.js'; |
| 9 | +import { Context } from '../commands/Context'; |
| 10 | +import { toFileURL } from '../../utils/resolve-file-url'; |
| 11 | +import { TranslatableCommandOptions } from '../i18n/Translation'; |
| 12 | + |
| 13 | +interface AppCommand { |
| 14 | + options: SlashCommandBuilder | Record<string, any>; |
| 15 | +} |
| 16 | + |
| 17 | +interface AppCommandMiddleware { |
| 18 | + beforeExecute: (ctx: Context) => Awaitable<unknown>; |
| 19 | + afterExecute: (ctx: Context) => Awaitable<unknown>; |
| 20 | +} |
| 21 | + |
| 22 | +interface LoadedCommand { |
| 23 | + command: ParsedCommand; |
| 24 | + data: AppCommand; |
| 25 | +} |
| 26 | + |
| 27 | +interface LoadedMiddleware { |
| 28 | + middleware: ParsedMiddleware; |
| 29 | + data: AppCommandMiddleware; |
| 30 | +} |
| 31 | + |
| 32 | +type CommandBuilderLike = |
| 33 | + | SlashCommandBuilder |
| 34 | + | ContextMenuCommandBuilder |
| 35 | + | Record<string, any>; |
| 36 | + |
| 37 | +const commandDataSchema = { |
| 38 | + command: (c: unknown) => |
| 39 | + c instanceof SlashCommandBuilder || |
| 40 | + c instanceof ContextMenuCommandBuilder || |
| 41 | + (c && typeof c === 'object'), |
| 42 | + chatInput: (c: unknown) => typeof c === 'function', |
| 43 | + autocomplete: (c: unknown) => typeof c === 'function', |
| 44 | + message: (c: unknown) => typeof c === 'function', |
| 45 | + messageContextMenu: (c: unknown) => typeof c === 'function', |
| 46 | + userContextMenu: (c: unknown) => typeof c === 'function', |
| 47 | +}; |
| 48 | + |
| 49 | +const middlewareDataSchema = { |
| 50 | + beforeExecute: (c: unknown) => typeof c === 'function', |
| 51 | + afterExecute: (c: unknown) => typeof c === 'function', |
| 52 | +}; |
| 53 | + |
| 54 | +export class AppCommandHandler { |
| 55 | + private loadedCommands = new Map<string, LoadedCommand>(); |
| 56 | + private loadedMiddlewares = new Map<string, LoadedMiddleware>(); |
| 57 | + |
| 58 | + public constructor(public readonly commandkit: CommandKit) {} |
| 59 | + |
| 60 | + public async prepareCommandRun(command: string) { |
| 61 | + const loadedCommand = this.loadedCommands.get(command); |
| 62 | + |
| 63 | + if (!loadedCommand) return null; |
| 64 | + |
| 65 | + return { |
| 66 | + command: loadedCommand, |
| 67 | + middlewares: loadedCommand.command.middlewares.map((m) => |
| 68 | + this.loadedMiddlewares.get(m), |
| 69 | + ), |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + public async loadCommands() { |
| 74 | + const commandsRouter = this.commandkit.commandsRouter; |
| 75 | + |
| 76 | + if (!commandsRouter) { |
| 77 | + throw new Error('Commands router has not yet initialized'); |
| 78 | + } |
| 79 | + |
| 80 | + const { commands, middleware } = commandsRouter.getData(); |
| 81 | + |
| 82 | + for (const [id, md] of middleware) { |
| 83 | + const data = await import(`${toFileURL(md.fullPath)}?t=${Date.now()}`); |
| 84 | + |
| 85 | + let handlerCount = 0; |
| 86 | + for (const [key, validator] of Object.entries(middlewareDataSchema)) { |
| 87 | + if (data[key] && !(await validator(data[key]))) { |
| 88 | + throw new Error( |
| 89 | + `Invalid export for middleware ${id}: ${key} does not match expected value`, |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + if (data[key]) handlerCount++; |
| 94 | + } |
| 95 | + |
| 96 | + if (handlerCount === 0) { |
| 97 | + throw new Error( |
| 98 | + `Invalid export for middleware ${id}: at least one handler function must be provided`, |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + this.loadedMiddlewares.set(id, { middleware: md, data }); |
| 103 | + } |
| 104 | + |
| 105 | + for (const [name, command] of commands) { |
| 106 | + const data = await import( |
| 107 | + `${toFileURL(command.fullPath)}?t=${Date.now()}` |
| 108 | + ); |
| 109 | + |
| 110 | + if (!data.command) { |
| 111 | + throw new Error( |
| 112 | + `Invalid export for command ${name}: no command definition found`, |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + let handlerCount = 0; |
| 117 | + |
| 118 | + for (const [key, validator] of Object.entries(commandDataSchema)) { |
| 119 | + if (key !== 'command' && data[key]) handlerCount++; |
| 120 | + if (data[key] && !(await validator(data[key]))) { |
| 121 | + throw new Error( |
| 122 | + `Invalid export for command ${name}: ${key} does not match expected value`, |
| 123 | + ); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (handlerCount === 0) { |
| 128 | + throw new Error( |
| 129 | + `Invalid export for command ${name}: at least one handler function must be provided`, |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + data.command = await this.applyLocalizations(data.command); |
| 134 | + |
| 135 | + this.loadedCommands.set(name, { command, data }); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public async applyLocalizations(command: CommandBuilderLike) { |
| 140 | + const localization = this.commandkit.config.localizationStrategy; |
| 141 | + |
| 142 | + const validLocales = Object.values(Locale).filter( |
| 143 | + (v) => typeof v === 'string', |
| 144 | + ); |
| 145 | + |
| 146 | + for (const locale of validLocales) { |
| 147 | + const translation = await localization.locateTranslation( |
| 148 | + command.name, |
| 149 | + locale, |
| 150 | + ); |
| 151 | + |
| 152 | + if (!translation?.command) continue; |
| 153 | + |
| 154 | + if (command instanceof SlashCommandBuilder) { |
| 155 | + if (translation.command.name) { |
| 156 | + command.setNameLocalization(locale, translation.command.name); |
| 157 | + } |
| 158 | + |
| 159 | + if (translation.command.description) { |
| 160 | + command.setDescriptionLocalization( |
| 161 | + locale, |
| 162 | + translation.command.description, |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + const raw = command.toJSON(); |
| 167 | + |
| 168 | + if (raw.options?.length && translation.command.options?.length) { |
| 169 | + const opt = translation.command.options.slice(); |
| 170 | + let o: TranslatableCommandOptions; |
| 171 | + |
| 172 | + while ((o = opt.shift()!)) { |
| 173 | + raw.options?.forEach((option) => { |
| 174 | + if (option.name === o.ref) { |
| 175 | + if (option.name) { |
| 176 | + option.name_localizations ??= {}; |
| 177 | + option.name_localizations[locale] = o.name; |
| 178 | + } |
| 179 | + |
| 180 | + if (option.description) { |
| 181 | + option.description_localizations ??= {}; |
| 182 | + option.description_localizations[locale] = o.description; |
| 183 | + } |
| 184 | + } |
| 185 | + }); |
| 186 | + } |
| 187 | + } |
| 188 | + } else if (command instanceof ContextMenuCommandBuilder) { |
| 189 | + if (translation.command.name) { |
| 190 | + command.setNameLocalization(locale, translation.command.name); |
| 191 | + } |
| 192 | + |
| 193 | + const raw = command.toJSON(); |
| 194 | + |
| 195 | + return raw; |
| 196 | + } else { |
| 197 | + command.name_localizations ??= {}; |
| 198 | + command.name_localizations[locale] = translation.command.name; |
| 199 | + |
| 200 | + if (command.description) { |
| 201 | + command.description_localizations ??= {}; |
| 202 | + command.description_localizations[locale] = |
| 203 | + translation.command.description; |
| 204 | + } |
| 205 | + |
| 206 | + if (command.options?.length && translation.command.options?.length) { |
| 207 | + const opt = translation.command.options.slice(); |
| 208 | + let o: TranslatableCommandOptions; |
| 209 | + |
| 210 | + while ((o = opt.shift()!)) { |
| 211 | + command.options.forEach((option: any) => { |
| 212 | + if (option.name === o.ref) { |
| 213 | + if (option.name) { |
| 214 | + option.name_localizations ??= {}; |
| 215 | + option.name_localizations[locale] = o.name; |
| 216 | + } |
| 217 | + |
| 218 | + if (option.description) { |
| 219 | + option.description_localizations ??= {}; |
| 220 | + option.description_localizations[locale] = o.description; |
| 221 | + } |
| 222 | + } |
| 223 | + }); |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | +} |
0 commit comments