'How do I translate discord message text if I need for my bot?
I want to be able to first check if the text already sent is in english, then if it is I want to just set that text to the content variable. If it is not in english already then I want to convert it to english then set it to the content variable.
Here is a necessary snippet of my code that is supposed to handle the check and translation:
import discord
from googletrans import Translator
translator = Translator()
message = await client.wait_for("message", check = check, timeout = 15)
beforecontent = message.content
#translate to english if needed
if translator.detect(beforecontent) == "en":
content = beforecontent
else:
new = translator.translate(beforecontent, dest='en')
print(new)
content = new.text
This in theory should work but instead I get these errors:
Traceback (most recent call last):
File "C:\Users\Wamy\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "F:\Projects Files\Rezi Bot\v1\bot.py", line 69, in grab
if translator.detect(beforecontent) == "en":
File "C:\Users\Wamy\AppData\Local\Programs\Python\Python39\lib\site-packages\googletrans\client.py", line 255, in detect
data = self._translate(text, 'en', 'auto', kwargs)
File "C:\Users\Wamy\AppData\Local\Programs\Python\Python39\lib\site-packages\googletrans\client.py", line 78, in _translate
token = self.token_acquirer.do(text)
File "C:\Users\Wamy\AppData\Local\Programs\Python\Python39\lib\site-packages\googletrans\gtoken.py", line 194, in do
self._update()
File "C:\Users\Wamy\AppData\Local\Programs\Python\Python39\lib\site-packages\googletrans\gtoken.py", line 62, in _update
code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Wamy\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Wamy\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Wamy\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'group'
What is going on and why does this not work?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
