From abb2625b198ee032608aac1bca0c53da79b61aee Mon Sep 17 00:00:00 2001 From: Lantium Dev Date: Tue, 4 Jan 2022 02:41:57 +0000 Subject: [PATCH] Envoie du Bot --- README.md | 38 ++++++++++++++++++ auth.json | 4 ++ heartbeat.js | 6 +++ index.js | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 29 ++++++++++++++ 5 files changed, 188 insertions(+) create mode 100644 README.md create mode 100644 auth.json create mode 100644 heartbeat.js create mode 100644 index.js create mode 100644 package.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..53998e5 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# DeepL - Traducteur automatique Discord + +Les joueurs ne doivent pas être séparés en raison de la barrière de la langue. + +## Usage + +Déployez cette application n'importe où. + + +### Données d'intentification + +Il y a une façon de définir les données secrètes : + +1. Mettre les ID dans le fichier `auth.json` + +Créer un fichier JSON qui contient les ID : + +``` +{ + "token": "le token du bot discord", + "auth_key" : "la clé API de DeepL" +} +``` + +Installez votre bot dans votre serveur discord, et lancez l'application. + +## Configuration de la langue cible + +Ajoutez cette ligne dans le sujet du salon dont vous voulez traduire les messages. + +``` +deepl-translate(EN) +``` + +Remplacez *EN* par la langue voulu. + +ATTENTION, tout les messages posté dans le salon sera traduit ! +Egalement, il faut 1 salon par langue. \ No newline at end of file diff --git a/auth.json b/auth.json new file mode 100644 index 0000000..15ab1c0 --- /dev/null +++ b/auth.json @@ -0,0 +1,4 @@ +{ + "token": "", + "auth_key" : "" +} diff --git a/heartbeat.js b/heartbeat.js new file mode 100644 index 0000000..1eb2aec --- /dev/null +++ b/heartbeat.js @@ -0,0 +1,6 @@ +const express = require('express'); +const app = express(); + +app.get('/heartbeat', (req, res) => res.send('alive')); + +app.listen(process.env.PORT, () => console.log("/heartbeat est à l'écoute.")); \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..00b5d1b --- /dev/null +++ b/index.js @@ -0,0 +1,111 @@ +const { Client, MessageEmbed } = require('discord.js'); +const axios = require('axios'); +const fs = require('fs'); + +const DEFAULT_LANG = 'FR'; +const AUTH_FILE = './auth.json'; + +const auth_key = require("./auth.json"); +const token = require("./auth.json"); + +const client = new Client(); + +client.once('ready', () => { + console.log('Ready!'); +}); + +let activites = ['Prêt pour vos traductions, Créer par Lantium !'], i = 0; + +setInterval(() => client.user.setPresence({ activity: { name: `${activites [i++ % activites.length]}`, type: 'PLAYING' }, status: 'online' }), 5000); + +client.on('message', message => { + + if (message.author.bot) return; + if (!message.channel.topic) return; + const translationConfig = message.channel.topic.trim().match(/deepl-translate\((.+)\)/); + if (!translationConfig) return; + + const target_lang = translationConfig[1]; + if (!target_lang) return; + + post(message.content, target_lang) + .then(response => { + + // if source text's language was same as target language, text was translated into default language. + if (response.data.translations.length === 1 && + response.data.translations[0].detected_source_language === target_lang) { + post(message.content, DEFAULT_LANG) + .then(retry => { + send(message, [ { lang: DEFAULT_LANG, translations: retry.data.translations } ]); + }) + + // if souce text's language was neither target language nor default language + // add default language translation. + } else if ( + response.data.translations[0].detected_source_language !== target_lang + && + response.data.translations[0].detected_source_language !== DEFAULT_LANG) { + post(message.content, DEFAULT_LANG) + .then(retry => { + send(message, + [ + { + lang: target_lang, + translations: response.data.translations + }, + { + lang: DEFAULT_LANG, + translations: retry.data.translations + } + ]); + }) + + // text was translated into target language. + } else { + send(message, [ { lang: target_lang, translations: response.data.translations } ]); + } + }) +}); + +const post = (message, lang) => { + return axios.post('https://api.deepl.com/v2/translate?' + + 'auth_key=' + auth_key +'&' + + 'text=' + encodeURIComponent(message) + '&' + + 'target_lang=' + lang) +} + +const send = (message, translations) => { + const embed = new MessageEmbed() + .setAuthor(message.author.username, message.author.avatarURL()) + .setColor(0xff0000) + .setDescription( + translations.map(t => { + let text = '`' + t.lang + ':` ' + t.translations[0].text; + if (t.translations.length > 1) { + text += ' ('; + text += t.translations.slice(1).map(others => + (others.detected_source_language + ': ' + others.text )) + .join(', '); + text += ')'; + } + return text; + }) + .join('\n')); + message.channel.send(embed); +} + +if (fs.existsSync(AUTH_FILE)) { + console.log('auth: using auth file.'); + var auth = require(AUTH_FILE); + token = auth.token; + auth_key = auth.auth_key; +} else { + console.log('auth: not found.'); + process.exit(1); +} + +if (process.env.KEEP_ALIVE_ENDPOINT) { + require('./heartbeat'); +} + +client.login(token); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..d350302 --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "lanbot-translate", + "version": "1.0.0", + "description": "Bot de traduction utilisant l'API de Deepl", + "main": "index.js", + "scripts": { + "start": "node index.js", + "test": "test" + }, + "engines": { + "node": "12.x" + }, + "repository": { + "type": "git", + "url": "git+https://gitlab.ataxya.net/lantium/lanbot-translate.git" + }, + "author": "lantium#9402", + "license": "MIT", + "bugs": { + "url": "https://gitlab.ataxya.net/lantium/lanbot-translate/-/issues" + }, + "homepage": "https://gitlab.ataxya.net/lantium/lanbot-translate/-/blob/master/README.md", + "dependencies": { + "axios": "^0.21.1", + "discord.js": "^12.5.3", + "eslint": "^7.32.0", + "express": "^4.17.1" + } +}