1 |
/** |
2 |
* This file is part of SudoBot. |
3 |
* |
4 |
* Copyright (C) 2021-2022 OSN Inc. |
5 |
* |
6 |
* SudoBot is free software; you can redistribute it and/or modify it |
7 |
* under the terms of the GNU Affero General Public License as published by |
8 |
* the Free Software Foundation, either version 3 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* SudoBot is distributed in the hope that it will be useful, but |
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU Affero General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU Affero General Public License |
17 |
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. |
18 |
*/ |
19 |
|
20 |
import { |
21 |
ApplicationCommandOptionChoiceData, |
22 |
AutocompleteInteraction, |
23 |
CacheType, |
24 |
CommandInteraction, |
25 |
Message, |
26 |
MessageContextMenuInteraction, |
27 |
User, |
28 |
} from "discord.js"; |
29 |
import BaseCommand from "../../utils/structures/BaseCommand"; |
30 |
import DiscordClient from "../../client/Client"; |
31 |
import CommandOptions from "../../types/CommandOptions"; |
32 |
import InteractionOptions from "../../types/InteractionOptions"; |
33 |
import { emoji } from "../../utils/Emoji"; |
34 |
import MessageEmbed from "../../client/MessageEmbed"; |
35 |
import AutoCompleteOptions from "../../types/AutoCompleteOptions"; |
36 |
import { readFileSync } from "fs"; |
37 |
import path from "path"; |
38 |
|
39 |
export default class TranslateCommand extends BaseCommand { |
40 |
supportsInteractions: boolean = true; |
41 |
supportsContextMenu = true; |
42 |
languages: Record<string, string> = {}; |
43 |
|
44 |
constructor() { |
45 |
super("translate", "settings", ["tr", "trnsl", "t", "Translate to English"]); |
46 |
this.languages = JSON.parse(readFileSync(path.resolve(__dirname, "..", "..", "..", "resources", "languages.json")).toString()); |
47 |
} |
48 |
|
49 |
async autoComplete(client: DiscordClient, interaction: AutocompleteInteraction<CacheType>, options: AutoCompleteOptions): Promise<void> { |
50 |
const focused = interaction.options.getFocused(); |
51 |
const matches: ApplicationCommandOptionChoiceData[] = []; |
52 |
|
53 |
for (const code in this.languages) { |
54 |
if (matches.length >= 24) { |
55 |
break; |
56 |
} |
57 |
|
58 |
if (code === focused || this.languages[code].includes(focused)) { |
59 |
matches.push({ |
60 |
name: this.languages[code], |
61 |
value: code, |
62 |
}); |
63 |
} |
64 |
} |
65 |
|
66 |
console.log(matches); |
67 |
|
68 |
interaction.respond(matches).catch(console.error); |
69 |
} |
70 |
|
71 |
async run( |
72 |
client: DiscordClient, |
73 |
message: Message | CommandInteraction | MessageContextMenuInteraction, |
74 |
options: CommandOptions | InteractionOptions |
75 |
) { |
76 |
if (!options.isInteraction && options.args[0] === undefined) { |
77 |
await message.reply(`${emoji("error")} You must specify the text to translate.`); |
78 |
return; |
79 |
} |
80 |
|
81 |
if (message instanceof MessageContextMenuInteraction && (!message.targetMessage.content || message.targetMessage.content.trim() === "")) { |
82 |
await message.reply(`${emoji("error")} The message does not have any text content. Note that embeds cannot be translated.`); |
83 |
return; |
84 |
} |
85 |
|
86 |
if (!(message instanceof Message)) |
87 |
await message.deferReply({ |
88 |
ephemeral: message instanceof CommandInteraction ? message.options.getBoolean("ephemeral") ?? false : false, |
89 |
}); |
90 |
|
91 |
const from = message instanceof CommandInteraction ? message.options.getString("from") ?? "auto" : "auto"; |
92 |
const to = message instanceof CommandInteraction ? message.options.getString("to") ?? "en" : "en"; |
93 |
|
94 |
console.log(from, to); |
95 |
|
96 |
if (from != 'auto' && !this.languages[from]) { |
97 |
await this.deferReply(message, ":x: Invalid language specified in the from option."); |
98 |
return; |
99 |
} |
100 |
|
101 |
if (to != 'auto' && !this.languages[to]) { |
102 |
await this.deferReply(message, ":x: Invalid language specified in the to option."); |
103 |
return; |
104 |
} |
105 |
|
106 |
const text = |
107 |
message instanceof Message |
108 |
? message.content.slice(client.config.props[message.guildId!].prefix.length).trim().slice(options.cmdName.length).trim() |
109 |
: message instanceof CommandInteraction |
110 |
? message.options.getString("text", true) |
111 |
: message.targetMessage.content; |
112 |
|
113 |
const { error, translation, response } = await client.translator.translate(text, from, to); |
114 |
|
115 |
if (error) { |
116 |
await this.deferReply(message, { |
117 |
embeds: [ |
118 |
new MessageEmbed({ |
119 |
color: 0xf14a60, |
120 |
author: { |
121 |
name: "Translation Failed", |
122 |
}, |
123 |
description: `${emoji("error")} Couldn't translate that due to an internal error.`, |
124 |
footer: { |
125 |
text: "Powered by Google Translate", |
126 |
}, |
127 |
}).setTimestamp(), |
128 |
], |
129 |
}); |
130 |
|
131 |
return; |
132 |
} |
133 |
|
134 |
await this.deferReply(message, { |
135 |
embeds: [ |
136 |
new MessageEmbed({ |
137 |
color: 0x007bff, |
138 |
author: { |
139 |
name: message instanceof MessageContextMenuInteraction ? (message.targetMessage.author as User).tag : "Translation", |
140 |
iconURL: |
141 |
message instanceof MessageContextMenuInteraction ? (message.targetMessage.author as User).displayAvatarURL() : undefined, |
142 |
}, |
143 |
description: translation, |
144 |
footer: { |
145 |
text: `Translated from ${this.languages[response!.data.src]} to ${this.languages[to]} • Powered by Google Translate`, |
146 |
}, |
147 |
}).setTimestamp(), |
148 |
], |
149 |
}); |
150 |
} |
151 |
} |