|
| 1 | +import { type Buildable, Command } from "@framework/commands/Command"; |
| 2 | +import { TakesArgument } from "@framework/arguments/ArgumentTypes"; |
| 3 | +import StringArgument from "@framework/arguments/StringArgument"; |
| 4 | +import { ErrorType } from "@framework/arguments/InvalidArgumentError"; |
| 5 | +import type Context from "@framework/commands/Context"; |
| 6 | +import { ContextType } from "@framework/commands/ContextType"; |
| 7 | +import ClassLoader from "@framework/import/ClassLoader"; |
| 8 | +import FatalError from "@main/core/FatalError"; |
| 9 | +import { GatewayEventListener } from "@framework/events/GatewayEventListener"; |
| 10 | +import { |
| 11 | + type ApplicationCommandOptionChoiceData, |
| 12 | + EmbedBuilder, |
| 13 | + type Interaction, |
| 14 | + User |
| 15 | +} from "discord.js"; |
| 16 | +import TranslationService from "@main/services/TranslationService"; |
| 17 | +import { Inject } from "@framework/container/Inject"; |
| 18 | +import RestStringArgument from "@framework/arguments/RestStringArgument"; |
| 19 | + |
| 20 | +type TranslateCommandArgs = { |
| 21 | + text: string; |
| 22 | +}; |
| 23 | + |
| 24 | +@TakesArgument<TranslateCommandArgs>({ |
| 25 | + names: ["text"], |
| 26 | + types: [RestStringArgument], |
| 27 | + optional: false, |
| 28 | + errorMessages: [ |
| 29 | + { |
| 30 | + [ErrorType.Required]: "You must provide text to translate." |
| 31 | + } |
| 32 | + ] |
| 33 | +}) |
| 34 | +class TranslateCommand extends Command<ContextType, true> { |
| 35 | + public override readonly name: string = "translate"; |
| 36 | + public override readonly description: string = "Translate text to another language."; |
| 37 | + public override readonly defer = true; |
| 38 | + public override readonly aliases = ["Translate to English"]; |
| 39 | + public override readonly usage = ["<text: String>"]; |
| 40 | + public override readonly supportedContexts = [ |
| 41 | + ContextType.ChatInput, |
| 42 | + ContextType.Legacy, |
| 43 | + ContextType.MessageContextMenu |
| 44 | + ]; |
| 45 | + protected readonly displayNames = new Intl.DisplayNames(["en"], { |
| 46 | + type: "language" |
| 47 | + }); |
| 48 | + protected readonly supportedLocales = Intl.DisplayNames.supportedLocalesOf(); |
| 49 | + private languages: Record<string, string> = {}; |
| 50 | + |
| 51 | + @Inject() |
| 52 | + private readonly translationService!: TranslationService; |
| 53 | + |
| 54 | + public override build(): Buildable[] { |
| 55 | + return [ |
| 56 | + this.buildChatInput() |
| 57 | + .addStringOption(option => |
| 58 | + option |
| 59 | + .setName("text") |
| 60 | + .setDescription("The text to translate.") |
| 61 | + .setRequired(true) |
| 62 | + ) |
| 63 | + .addStringOption(option => |
| 64 | + option |
| 65 | + .setName("to") |
| 66 | + .setDescription("The language to translate to.") |
| 67 | + .setAutocomplete(true) |
| 68 | + ) |
| 69 | + .addStringOption(option => |
| 70 | + option |
| 71 | + .setName("from") |
| 72 | + .setDescription("The language to translate from.") |
| 73 | + .setAutocomplete(true) |
| 74 | + ) |
| 75 | + ]; |
| 76 | + } |
| 77 | + |
| 78 | + public override async initialize(): Promise<void> { |
| 79 | + using file = await ClassLoader.getInstance(this.application).getResource("languages.json"); |
| 80 | + |
| 81 | + if (!file) { |
| 82 | + throw new FatalError("Failed to load languages.json."); |
| 83 | + } |
| 84 | + |
| 85 | + this.languages = await file.readJson(); |
| 86 | + } |
| 87 | + |
| 88 | + @GatewayEventListener("interactionCreate") |
| 89 | + public async onInteractionCreate(interaction: Interaction) { |
| 90 | + if (!interaction.isAutocomplete() || interaction.commandName !== this.name) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const focused = interaction.options.getFocused(); |
| 95 | + const matches: ApplicationCommandOptionChoiceData[] = []; |
| 96 | + |
| 97 | + for (const code in this.languages) { |
| 98 | + if (matches.length >= 25) { |
| 99 | + break; |
| 100 | + } |
| 101 | + |
| 102 | + if (code === focused || this.languages[code].includes(focused)) { |
| 103 | + matches.push({ |
| 104 | + name: this.languages[code], |
| 105 | + value: code |
| 106 | + }); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (matches.length < 25) { |
| 111 | + for (const locale of this.supportedLocales) { |
| 112 | + if (matches.length >= 25) { |
| 113 | + break; |
| 114 | + } |
| 115 | + |
| 116 | + if (this.languages[locale]) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + const displayName = this.displayNames.of(locale); |
| 121 | + |
| 122 | + if (!displayName) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + if (locale === focused || displayName.includes(focused)) { |
| 127 | + matches.push({ |
| 128 | + name: displayName, |
| 129 | + value: locale |
| 130 | + }); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + interaction.respond(matches).catch(this.application.logger.error); |
| 136 | + } |
| 137 | + |
| 138 | + public override async execute( |
| 139 | + context: Context, |
| 140 | + args: TranslateCommandArgs | undefined |
| 141 | + ): Promise<void> { |
| 142 | + const text = context.isMessageContextMenu() |
| 143 | + ? context.commandMessage.targetMessage.content |
| 144 | + : args?.text; |
| 145 | + |
| 146 | + if (!text) { |
| 147 | + await context.error( |
| 148 | + context.isMessageContextMenu() |
| 149 | + ? "The message does not contain any plain text." |
| 150 | + : "You must provide text to translate." |
| 151 | + ); |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + const to = (context.isChatInput() ? context.options.getString("to") : null) ?? "en"; |
| 156 | + const from = (context.isChatInput() ? context.options.getString("from") : null) ?? "auto"; |
| 157 | + |
| 158 | + try { |
| 159 | + if (from !== "auto" && !this.languages[from] && !this.displayNames.of(from)) { |
| 160 | + throw new Error(); |
| 161 | + } |
| 162 | + } |
| 163 | + catch { |
| 164 | + await context.error("Invalid language specified in the `from` option"); |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + try { |
| 169 | + if (to !== "auto" && !this.languages[to] && !this.displayNames.of(to)) { |
| 170 | + throw new Error(); |
| 171 | + } |
| 172 | + } |
| 173 | + catch { |
| 174 | + await context.error("Invalid language specified in the `to` option"); |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + const toString = this.displayNames.of(to); |
| 179 | + const { error, translation, response } = await this.translationService.translate( |
| 180 | + text, |
| 181 | + from, |
| 182 | + to |
| 183 | + ); |
| 184 | + |
| 185 | + if (error) { |
| 186 | + await context.reply({ |
| 187 | + embeds: [ |
| 188 | + new EmbedBuilder({ |
| 189 | + color: 0xf14a60, |
| 190 | + author: { |
| 191 | + name: "Translation Failed" |
| 192 | + }, |
| 193 | + description: `${context.emoji("error") ?? ""} Couldn't translate that due to an internal error.`, |
| 194 | + footer: { |
| 195 | + text: "Powered by Google Translate" |
| 196 | + } |
| 197 | + }).setTimestamp() |
| 198 | + ] |
| 199 | + }); |
| 200 | + |
| 201 | + return; |
| 202 | + } |
| 203 | + |
| 204 | + const fromString = this.displayNames.of(response!.data.src); |
| 205 | + |
| 206 | + await context.reply({ |
| 207 | + embeds: [ |
| 208 | + new EmbedBuilder({ |
| 209 | + color: 0x007bff, |
| 210 | + author: { |
| 211 | + name: context.isMessageContextMenu() |
| 212 | + ? (context.commandMessage.targetMessage.author as User).username |
| 213 | + : "Translation", |
| 214 | + iconURL: context.isMessageContextMenu() |
| 215 | + ? ( |
| 216 | + context.commandMessage.targetMessage.author as User |
| 217 | + ).displayAvatarURL() |
| 218 | + : undefined |
| 219 | + }, |
| 220 | + description: translation, |
| 221 | + footer: { |
| 222 | + text: `Translated from ${fromString ?? this.languages[response!.data.src] ?? response!.data.src} to ${ |
| 223 | + toString ?? this.languages[to] ?? to |
| 224 | + } • Powered by Google Translate` |
| 225 | + } |
| 226 | + }) |
| 227 | + ] |
| 228 | + }); |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +export default TranslateCommand; |
0 commit comments