I suddenly wanted to translate English sentences, but I found it troublesome to add chrome on my PC, open google translate, and type. However, Discord is always on for some reason, so I thought I would use Discord's translation BOT, but I felt that downloading it as it was was something different, so I decided to make it and came here. This is the second BOT in my life.
――Translates Japanese into English ――Translates English into Japanese --Translates to Japanese without specifying a language other than Japanese --If you specify the language, it will translate from any word to any word.
--Translate to Japanese with ! Trans [character string other than Japanese]
--Translate Japanese to English with ! Trans [Japanese string]
--! Trans [Language name A]-[Language name B]-[Character string of A word]
to translate A word to B word
--Get the language code of that string with ! detect [arbitrary string]
--Environment where Python can be used --Basic knowledge of Python --Easy way to make a bot
This is easy to understand. How to make a simple Discord Bot with Python Discord Bot fastest tutorial [Python & Heroku & GitHub]
A free library that allows you to translate and detect languages using the Google Translate API. However, it is not very stable and can be blocked by Google at any time, so if you want stability, we recommend using the official API of Google Cloud.
pip install googletrans
You can install it with.
Successfully installed googletrans- (arbitrary version)
Is completed.
As an example, let's create a program that converts Japanese to English.
trans_test.py
from googletrans import Translator
translator = Translator()
japanese = translator.translate('Good morning.')
print(japanese.text)
#>> Good morning.
In addition to text, the value returned by translete () includes the language of the conversion source and the language after conversion.
trans_test.py
from googletrans import Translator
translator = Translator()
japanese = translator.translate('Good morning.')
print(japanese)
#>> Translated(src=ja, dest=en, text=Good morning., pronunciation=None, extra_data="{'translat...")
To detect the language, put the sentence or word you want to detect in the parentheses of detect ().
trans_test.py
from googletrans import Translator
translator = Translator()
detect = translator.detect('Good morning.')
print(detect)
#>> Detected(lang=ja, confidence=1.0)
The two letters in English after lang are the language name, and confidence is the accuracy.
What a useful library! !! This time, we will make a translation BOT using this library.
First, put the finished product down.
main.py
import discord
from googletrans import Translator
TOKEN = 'Tokens face down'
client = discord.Client()
translator = Translator()
@client.event
async def on_ready():
print('--------------')
print('You are now logged')
print(client.user.name)
print(client.user.id)
print('--------------')
@client.event
async def on_message(message):
if message.author.bot:
return
if message.content.startswith('!trans'):
say = message.content
say = say[7:]
if say.find('-') == -1:
str = say
detact = translator.detect(str)
befor_lang = detact.lang
if befor_lang == 'ja':
convert_string = translator.translate(str, src=befor_lang, dest='en')
embed = discord.Embed(title='Conversion result', color=0xff0000)
embed.add_field(name='Befor', value=str)
embed.add_field(name='After', value=convert_string.text, inline=False)
await message.channel.send(embed=embed)
else:
convert_string = translator.translate(str, src=befor_lang, dest='ja')
embed = discord.Embed(title='Conversion result', color=0xff0000)
embed.add_field(name='Befor', value=str)
embed.add_field(name='After', value=convert_string.text, inline=False)
await message.channel.send(embed=embed)
else:
trans, str = list(say.split('='))
befor_lang, after_lang = list(trans.split('-'))
convert_string = translator.translate(str, src=befor_lang, dest=after_lang)
embed = discord.Embed(title='Conversion result', color=0xff0000)
embed.add_field(name='Befor', value=str)
embed.add_field(name='After', value=convert_string.text, inline=False)
await message.channel.send(embed=embed)
if message.content.startswith('!detect'):
say = message.content
s = say[8:]
detect = translator.detect(s)
m = 'The language of this string is probably' + detect.lang + 'is.'
await message.channel.send(m)
client.run(TOKEN)
I don't know if it looks complicated, but it's very simple to do.
①
!trans ~
When the instruction starts with, remove the instruction ! Trans
and one half-width character from the instruction.
↓
detect the language of the sentence with detect ().
↓
If the language is Japanese, translate it into English, otherwise translate it into Japanese.
② (Assuming that! Trans has already been removed)
!trans [A]-[B]-[C]
At the time of the instruction, A is the language name before conversion, B is the language name after conversion, and C is a character string.
↓
Translate C from A, B, C to B language.
③
!detect ~
When the command starts with, remove the command ! Detect
and one half-width character from the command.
↓
detect () to detect the language of the sentence and output it
That's all for the overall movement. There are places where ʻembed ~ `is written in various places, but this is necessary for embedding when speaking on Discord. Embedding seems to be easier to see.
There is nothing wrong with the content to be translated. I choose it appropriately.
--From Japanese to English
--From English to Japanese
--From German to Japanese
--From Japanese to German
--Language detection Japanese
French
I've implemented everything I was trying to do!
I think the translation is inferior to google translate, but I am satisfied because I was able to do what I was trying to do. I'm still doing something elementary, so I have a lot of things I want to do, such as voice chat, acquiring members, and combining it with web scraping.
Let's make it because everyone is happy to make BOT! !!
If you make a mistake or have any improvements, feel free to tell me anything on Twitter.
Recommended Posts