[GO] I made a Discord bot in Python that translates when it reacts

A bot like this image.png

The method of making a bot with Discord is omitted. Please read around here. Practical Discord Bot in Python (discordpy explanation)

Source code

#Installed discord.Load py
import discord
from googletrans import Translator

#Replace with your bot's access token
TOKEN = '[bot-token]'

#Generate the objects needed for the connection
client = discord.Client()

#Processing that operates at startup
@client.event
async def on_ready():
    #When started, a login notification will be displayed in the terminal
    print('You are now logged')

@client.event
async def on_reaction_add(reaction, user):
    print("emoji-id")
    print(reaction.emoji.id)
    if reaction.count == 1:
        #Japanese translation
        if reaction.emoji.id == 687336060556017758:
            translator = Translator()
            trans_en = translator.translate(reaction.message.content, src='en', dest='ja')
            await reaction.message.channel.send(trans_en.text)


        #English translation
        if reaction.emoji.id == 687336087408214062:
            translator = Translator()
            trans_en = translator.translate(reaction.message.content, src='ja', dest='en')
            await reaction.message.channel.send(trans_en.text)



#Launching a bot and connecting to a Discord server
client.run(TOKEN)

[bot-token] is the token of each bot. For [emoji-id], set the ID of the emoji you want to react to (Integer).

Google Translate "googletrans"

All you have to do is throw the translation to Google Translate and return the result in a message. Regarding emoji.id, if it is *** registered emoji ***, the emoji ID is assigned, so after setting the emoji on the server, react and solidify the emoji ID spit out in print. Let's write in. In my case, I translated the Japanese flag into Japanese and the American flag into English.

Since src is the sentence before translation and dest is the sentence after translation, it can be translated into English or Japanese.

When completed, deploy it around heroku and run it, and it will translate 24 hours a day, convenient! By the way, I didn't know how to get the number of reactions, so if I react 2 times, it will be translated twice.

Please tell me how to do it.

Postscript: I was told how to make one reaction

if reaction.count == 1: Thank you @rareshana!

reference

Practical Discord Bot in Python (discordpy explanation) How to detect reaction with Discord.py & list of parameters [Python] Try converting (translating) Japanese data to English using googletrans

Recommended Posts

I made a Discord bot in Python that translates when it reacts
A template that I often use when making Discord BOT in Python (memorial note)
I made a discord bot
In Python, I made a LINE Bot that sends pollen information from location information.
When I made a Discord Bot, my classmates destroyed my computer
I made a payroll program in Python!
[Python] I made a Line bot that randomly asks English words.
A memo that I wrote a quicksort in Python
When I try matplotlib in Python, it says'cairo.Context'
[IOS] I made a widget that displays Qiita trends in Pythonista3. [Python]
I made a Caesar cryptographic program in Python.
I made a Mattermost bot with Python (+ Flask)
[Python] I made a bot that tells me the current temperature when I enter a place name on LINE
I made a Line Bot that uses Python to retrieve unread Gmail emails!
[Python] I made a LINE Bot that detects faces and performs mosaic processing.
I made a familiar function that can be used in statistics with Python
A story that I was addicted to when I made SFTP communication with python
I made a Twitter BOT with GAE (python) (with a reference)
I made a prime number generation program in Python
I made a VM that runs OpenCV for Python
What's in that variable (when running a Python script)
I made a LINE BOT with Python and Heroku
I made a prime number generation program in Python 2
I made a python text
A Python beginner made a chat bot, so I tried to summarize how to make it.
A story that stumbled when I made a chatbot with Transformer
I made a simple typing game with tkinter in Python
I made a LINE BOT that returns parrots with Go
I made a quick feed reader using feedparser in Python
I tried "a program that removes duplicate statements in Python"
I made a puzzle game (like) with Tkinter in Python
I made a tool in Python that right-clicks an Excel file and divides it into files for each sheet.
[Python] I made a function that decrypts AES encryption just by throwing it with pycrypto.
I made a Line-bot using Python!
I made a wikipedia gacha bot
A story that went missing when I specified a path starting with a tilde (~) in python open
I made a program to collect images in tweets that I liked on twitter with Python
I made a daemon with Python
When writing a program in Python
I wrote a class that makes it easier to divide by specifying part of speech when using Mecab in python
[Python3] I made a decorator that declares undefined functions and methods.
I made a package that can compare morphological analyzers with Python
I made a program that solves the spot the difference in seconds
Easy! Implement a Twitter bot that runs on Heroku in Python
I made a Twitter bot that mutters Pokemon caught by #PokemonGO
I made a shuffle that can be reset (reverted) with Python
I get a can't set attribute when using @property in python
I made a slack bot that notifies me of the temperature
I made a library that adds docstring to a Python stub file.
I made a Discord chat reading bot by imitating the appearance
I made a segment tree with python, so I will introduce it
[python] I made a class that can write a file tree quickly
I made a garbled generator that encodes favorite sentences from UTF-8 to Shift-JIS (cp932) in Python
A story that didn't work when I tried to log in with the Python requests module
I made a character counter with Python
I made a Hex map with Python
After studying Python3, I made a Slackbot
I created a password tool in Python.
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python