Discord Translation Bot AttributeError:“ NoneType”对象没有属性“ group”

时间:2020-11-04 13:33:23

标签: python discord bots google-translation-api

所以我正在尝试用python编写一个discord bot,它运行得很好,它可以很好地跨2个不同的通道转换大多数消息,但是在某些消息上我得到了AttributeError:'NoneType'对象没有属性'group'< / p>

我的代码是:

@client.event
async def on_message(message):

dutch_channel = 123456789
english_channel = 123456789

translator = googletrans.Translator()
text = message.content

if message.author == client.user:
    return

if message.channel.id == dutch_channel:
    
    try:

        text_translated = translator.translate(text, dest = 'english')

        channel = client.get_channel(english_channel)
        await channel.send(f'__**{message.author.name}**__ \r\n {text_translated.text}')
    except AttributeError: 

        print(message.content)

if message.channel.id == english_channel:

    try:

        text_translated = translator.translate(text, dest = 'dutch')

        channel = client.get_channel(dutch_channel) 
        await channel.send(f'__**{message.author.name}**__ \r\n {text_translated.text}')
    except AttributeError:

        print(message.content)

await client.process_commands(message)

我认为这是因为特殊字符而引起的,至少我知道特殊字符总是会引起问题。

其他信息:

完整的堆栈跟踪:

message
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Wessel\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\Wessel\Desktop\PythonBot\bot.py", line 88, in on_message
    output = f' \r\n {text_translated.text}'
UnboundLocalError: local variable 'text_translated' referenced before assignment

Discord.py版本1.5.1 GoogleTrans版本3.0.0

0 个答案:

没有答案