Envoie du Bot

This commit is contained in:
Lantium 2022-01-04 02:41:57 +00:00
commit abb2625b19
5 changed files with 188 additions and 0 deletions

38
README.md Normal file
View File

@ -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.

4
auth.json Normal file
View File

@ -0,0 +1,4 @@
{
"token": "",
"auth_key" : ""
}

6
heartbeat.js Normal file
View File

@ -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."));

111
index.js Normal file
View File

@ -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);

29
package.json Normal file
View File

@ -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"
}
}