From ec64dbf3f1b9b18d68e10aabfb6ca49a9bff82d0 Mon Sep 17 00:00:00 2001 From: Superkooka Date: Wed, 3 Aug 2022 21:02:14 +0200 Subject: [PATCH] feat: translate command --- README.md | 30 ++++++++++++++++-- config.exemple.toml | 3 ++ package.json | 1 + src/commands/translate.ts | 67 +++++++++++++++++++++++++++++++++++++++ src/config.ts | 5 +++ src/index.ts | 10 ++++++ src/utils/deepl.ts | 19 +++++++++++ yarn.lock | 30 +++++++++++++++++- 8 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 src/commands/translate.ts create mode 100644 src/utils/deepl.ts diff --git a/README.md b/README.md index c73d422..beefb68 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,24 @@ # Discord Bot Template -Discord Bot Template for Another Child Spring server +Translate command for Another Child Spring server + +## Command Usage + +### Name + +translate - Translate a message into the selected language + +### Syntax + +translate [lang] [message] + +### Description + +The translate command will call the Deepl API to translate the message into the chosen lang. The lang parameter can simply be the destination language (source language is automatically detected) but if the user wants to specify the source language it can be formatted like so: `src->dst` + +## Limitation + +- Deepl API limit to 500.000 characters/month for the free version ## Requirement @@ -9,6 +27,13 @@ Discord Bot Template for Another Child Spring server ## How to install +### DeeplAPI + +1. Create an account on [Deepl API](https://www.deepl.com/pro-api) +2. Generate a token + +### Project + 1. Clone the repo 2. Copy the `config.exemple.toml` and rename it `config.toml` 3. Launch the docker container with `make start` or `docker-compose up -d` @@ -19,7 +44,8 @@ Discord Bot Template for Another Child Spring server ## Notes - Configuration file format is [TOML](https://toml.io/en/). It is a minimal configuration file format that's easy to read due to obvious semantics. TOML is designed to map unambiguously to a hash table. TOML should be easy to parse into data structures in a wide variety of languages. Read `src/config.ts` to see all possibilities. +- Project use [Inhibitor](https://sheweny.js.org/guide/inhibitors/Inhibitor.html) from `Sheweny`. Inhibitor is a kind of middleware for command. Inhibitors allow you to limit the use of a command, an event, or an interaction. ## Contributions/License -This project has an AGPLv3 license. This project use the `Sheweny` framework with `discord.js` underlying and `BinaryMuse/toml-node` for configuration parsing. +This project has an AGPLv3 license. This project use the `Sheweny` framework with `discord.js` underlying and `BinaryMuse/toml-node` for configuration parsing. To communicate with DeeplAPI, project use the [offical javascript wrapper](https://www.npmjs.com/package/deepl-node). diff --git a/config.exemple.toml b/config.exemple.toml index d09e785..f755384 100644 --- a/config.exemple.toml +++ b/config.exemple.toml @@ -1 +1,4 @@ token = "DISCORD TOKEN" + +[deepl] +token = "DEEPL TOKEN" diff --git a/package.json b/package.json index 98e2222..d87ae6f 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "index.js", "license": "AGPL-3.0-only", "dependencies": { + "deepl-node": "^1.3.1", "discord.js": "^13.8.1", "sheweny": "^3.4.3", "toml": "^3.0.0" diff --git a/src/commands/translate.ts b/src/commands/translate.ts new file mode 100644 index 0000000..d67bda2 --- /dev/null +++ b/src/commands/translate.ts @@ -0,0 +1,67 @@ +import { Command } from "sheweny"; +import { TextResult, Translator } from "deepl-node"; +import type { ShewenyClient } from "sheweny"; +import type { AutocompleteInteraction, CommandInteraction } from "discord.js"; +import { Container } from "../container"; +import { isTargetLanguageCode, targetLanguageCode, targetLanguageName } from "../utils/deepl"; + +export class TranslateCommand extends Command { + constructor(client: ShewenyClient) { + super(client, { + name: "translate", + description: "Translate a message into the selected language", + type: "SLASH_COMMAND", + options: [ + { + name: "lang", + description: "Destination language", + type: "STRING", + required: true, + autocomplete: true, + }, + { + name: "message", + description: "Message to be translated", + type: "STRING", + required: true + }, + ] + }); + } + + async execute(interaction: CommandInteraction) { + const lang = interaction.options.get("lang")?.value + const message = interaction.options.get("message")?.value + + if (typeof lang != "string" || typeof message != "string") { + throw new Error("plop") + } + + const translator = Container.container.get("translator") + + if (!isTargetLanguageCode(lang)) { + interaction.reply({ content: "Invalid language", ephemeral: true }) + return + } + + const result: TextResult = await translator.translateText(message, null, lang) + + interaction.reply({ content: result.text }); + } + + onAutocomplete(interaction: AutocompleteInteraction) { + const userInput = interaction.options.getFocused(true).value + + const choices: [string, string][] = Object.entries( + targetLanguageName.reduce((obj, key, index) => ({ ...obj, [key]: targetLanguageCode[index] }), {}) + ) + + const filtered = choices + .filter(([key, _]) => key.startsWith(userInput)) + .sort() + .slice(0, 25) + .map(([name, code]) => ({ name: name, value: code })) + + interaction.respond(filtered) + } +} diff --git a/src/config.ts b/src/config.ts index 60be932..6fb645f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,3 +1,8 @@ export interface Config { token: string + deepl: DeeplSection +} + +interface DeeplSection { + token: string, } diff --git a/src/index.ts b/src/index.ts index 87a821e..df73b54 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { ShewenyClient } from "sheweny"; +import {Translator} from "deepl-node"; import {Container} from "./container"; import {Config} from "./config"; import * as TOML from "toml"; @@ -7,12 +8,21 @@ import * as fs from "fs"; const configurationFile = fs.readFileSync("./config.toml").toString() const configuration = TOML.parse(configurationFile) as Config +const translator = new Translator(configuration.deepl.token) + const container = new Container() container.set("config", configuration) +container.set("translator", translator) Container.container = container const client = new ShewenyClient({ intents: ["GUILDS", "GUILD_MEMBERS", "GUILD_MESSAGES"], + managers: { + commands: { + directory: "commands/", + loadAll: true + } + } }); client.login(configuration.token) diff --git a/src/utils/deepl.ts b/src/utils/deepl.ts new file mode 100644 index 0000000..9a3d15a --- /dev/null +++ b/src/utils/deepl.ts @@ -0,0 +1,19 @@ +import {SourceLanguageCode, TargetLanguageCode} from "deepl-node"; + +export const commonLanguageCode = ["bg", "cs", "da", "de", "el", "es", "et", "fi", "fr", "hu", "id", "it", "ja", "lt", "lv", "nl", "pl", "ro", "ru", "sk", "sl", "sv", "tr", "zh"] +export const commomLanguageName = ["Bulgarian", "Czech", "Danish", "German", "Greek", "Spanish", "Estonian", "Finnish", "French", "Hungarian", "Indonesian", "Italian", "Japanese", + "Lithuanian", "Latvian", "Dutch", "Polish", "Romanian", "Russian", "Slovak", "Slovenian", "Swedish", "Turkish", "Chinese"] + +export const sourceLanguageCode = [...commonLanguageCode, "en", "pt"] +export const sourceLanguageName = [...commomLanguageName, "English", "Portugese"] + +export const targetLanguageCode = [...commonLanguageCode, "en-GB", 'en-US', 'pt-BR', 'pt-PT'] +export const targetLanguageName = [...commomLanguageName, "English (UK)", "English (US)", "Portuguese (Brazil)", "Portuguese (Portugal)"] + +export function isSourceLanguageCode(code: string): code is SourceLanguageCode { + return sourceLanguageCode.includes(code) +} + +export function isTargetLanguageCode(code: string): code is TargetLanguageCode { + return targetLanguageCode.includes(code) +} diff --git a/yarn.lock b/yarn.lock index 62a68e9..2f082c8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -47,7 +47,7 @@ "@types/node" "*" form-data "^3.0.0" -"@types/node@*": +"@types/node@*", "@types/node@>=12.0": version "18.0.1" resolved "https://registry.yarnpkg.com/@types/node/-/node-18.0.1.tgz#e91bd73239b338557a84d1f67f7b9e0f25643870" integrity sha512-CmR8+Tsy95hhwtZBKJBs0/FFq4XX7sDZHlGGf+0q+BRZfMbOTkzkj0AFAuTyXbObDIoanaBBW0+KEW+m3N16Wg== @@ -87,6 +87,14 @@ asynckit@^0.4.0: resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== +axios@>=0.21.2: + version "0.27.2" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972" + integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ== + dependencies: + follow-redirects "^1.14.9" + form-data "^4.0.0" + balanced-match@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" @@ -149,6 +157,16 @@ create-require@^1.1.0: resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333" integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ== +deepl-node@^1.3.1: + version "1.3.1" + resolved "https://registry.yarnpkg.com/deepl-node/-/deepl-node-1.3.1.tgz#1cc05affdc3af12fb393e295c5333cdcc2b10c22" + integrity sha512-Q41JzD/nxDhDZcJ5RqQfQY8hphX/ZadO/khH4H0WsAGdEwwpZ9By+dqD2si338Qajj7a9ePYYVGmCl9nIxx+wA== + dependencies: + "@types/node" ">=12.0" + axios ">=0.21.2" + form-data "^3.0.0" + loglevel ">=1.6.2" + delayed-stream@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" @@ -198,6 +216,11 @@ fill-range@^7.0.1: dependencies: to-regex-range "^5.0.1" +follow-redirects@^1.14.9: + version "1.15.1" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.1.tgz#0ca6a452306c9b276e4d3127483e29575e207ad5" + integrity sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA== + form-data@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.1.tgz#ebd53791b78356a99af9a300d4282c4d5eb9755f" @@ -301,6 +324,11 @@ is-number@^7.0.0: resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b" integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng== +loglevel@>=1.6.2: + version "1.8.0" + resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.8.0.tgz#e7ec73a57e1e7b419cb6c6ac06bf050b67356114" + integrity sha512-G6A/nJLRgWOuuwdNuA6koovfEV1YpqqAG4pRUlFaz3jj2QNZ8M4vBqnVA+HBTmU/AMNUtlOsMmSpF6NyOjztbA== + make-error@^1.1.1: version "1.3.6" resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"