I’ve been trying to make my discord bot translate texts using a module called googletrans. It seems fairly simple and it should have worked without any hassle, or so I thought.
So after my import statements, I have translator = Translator()
.
My following cog code is:
JavaScript
x
8
1
@commands.command(aliases=["tl", "Tl", "Translate"])
2
async def translate(self, ctx, *, message):
3
language = translator.detect(message)
4
translation = translator.translate(message)
5
embed = discord.Embed(color=discord.Color.dark_theme())
6
embed.add_field(name=f"Language: {language} ", value=f'{translation}')
7
await ctx.send(embed=embed)
8
But it shows this error: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'group'.
Where am I going wrong? Any help would be much appreciated!
Edit: the full traceback:
JavaScript
1
27
27
1
Ignoring exception in command translate:
2
Traceback (most recent call last):
3
File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesdiscordextcommandscore.py", line 85, in wrapped
4
ret = await coro(*args, **kwargs)
5
File "C:Userswave computerPycharmProjectspythonProjectcogstranslate.py", line 13, in translate
6
language = translator.detect(message)
7
File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesgoogletransclient.py", line 255, in detect
8
data = self._translate(text, 'en', 'auto', kwargs)
9
File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesgoogletransclient.py", line 78, in _translate
10
token = self.token_acquirer.do(text)
11
File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesgoogletransgtoken.py", line 194, in do
12
self._update()
13
File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesgoogletransgtoken.py", line 62, in _update
14
code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
15
AttributeError: 'NoneType' object has no attribute 'group'
16
17
The above exception was the direct cause of the following exception:
18
19
Traceback (most recent call last):
20
File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesdiscordextcommandsbot.py", line 902, in invoke
21
await ctx.command.invoke(ctx)
22
File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesdiscordextcommandscore.py", line 864, in invoke
23
await injected(*ctx.args, **ctx.kwargs)
24
File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesdiscordextcommandscore.py", line 94, in wrapped
25
raise CommandInvokeError(exc) from exc
26
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'group'
27
Advertisement
Answer
I had a same problem. Installing alpha version of google trans helped, so try doing this:
JavaScript
1
2
1
pip install googletrans==3.1.0a0
2
And:
JavaScript
1
8
1
from googletrans import Translator
2
3
@client.command()
4
async def translate(ctx, lang, *, thing):
5
translator = Translator()
6
translation = translator.translate(thing, dest=lang)
7
await ctx.send(translation.text)
8