Hey everyone, I’m still having trouble with my Discord bot. I thought I fixed the issue by changing sendMessage to sentMessage, but I’m getting a new error now. Here’s my code:
@bot.event
async def on_reaction_add(reaction, user):
if reaction.emoji:
emoji_reacted = reaction.emoji
if emoji_reacted not in language_dict:
await reaction.message.channel.send('Language not recognized!')
else:
target_lang = language_dict[emoji_reacted]
original_text = str(reaction.message.content)
translator = Translator()
translated_text = translator.translate(original_text, dest=target_lang).text
await reaction.message.channel.send(f'Translation: {translated_text}')
The error I’m getting is:
line 224, in on_reaction_add
translated_text = translator.translate(original_text, dest=target_lang).text
AttributeError: 'coroutine' object has no attribute 'text'
Can anyone help me figure out what’s going wrong? I’m really stuck on this one.
I’ve been there, mate. Async stuff can be a real pain with Discord bots. From what I can see, your issue is likely with the Translator library you’re using. It’s probably not designed for async operations, which is why you’re getting that coroutine error.
Here’s what worked for me: ditch the unofficial googletrans library and go for the official Google Cloud Translation API. Yeah, it’s a bit more work to set up initially - you’ll need to get an API key and all that - but trust me, it’s worth it in the long run. It plays nice with async operations and is way more reliable.
If you’re not keen on switching APIs, you could try wrapping your translation call in an executor to run it in a separate thread. Something like:
translated_text = await bot.loop.run_in_executor(None, lambda: translator.translate(original_text, dest=target_lang).text)
This might help bypass the async issues you’re facing. Good luck with your bot, and don’t hesitate to ask if you need more help!
I’ve run into similar issues with translation APIs in Discord bots. The error you’re seeing suggests the translate method is returning a coroutine instead of the translated text directly. This often happens with async libraries.
One solution is to wrap the translation call in an executor, like this:
translated_text = await bot.loop.run_in_executor(None, lambda: translator.translate(original_text, dest=target_lang).text)
This runs the translation in a separate thread, avoiding async issues.
Alternatively, consider switching to the official Google Cloud Translation API. It’s more reliable for async operations, though it requires some setup with API keys. In my experience, it’s worth the effort for a smoother bot experience.
Remember to handle API rate limits and errors gracefully in your code. Good luck with your bot!
hey mate, i had similar issue with google translate. try using the official Google Cloud Translation API instead of googletrans. it’s better for async stuff. you might need to sign up for an API key, but it’s worth it. good luck!