简体   繁体   中英

Could someone help me find the error in this code?

Here is my code:

from discord.ext import commands
import os
import discord
import requests
import json



my_secret = os.environ['MY_TOKEN']
GUILD = os.getenv('DISCORD_GUILD')

bot = commands.Bot(command_prefix='!') #prefix

discord.CustomActivity("Jaxon\'s brain melt", ActivityType="watching", emoji=None)

@bot.command(name="enfr", help="Translates text from english to french")
async def enfr(ctx, text):
  response = requests.get("https://api.mymemory.translated.net/get?q=" + text + "&langpair=en|fr")
  json_data = json.loads(response.text)
  translation = json_data.get("translatedText") + "test"
  print(response.status_code)
  await ctx.channel.send(translation)

bot.run(my_secret)

This is the error I get:

200
Ignoring exception in command enfr:
Traceback (most recent call last):
  File "/home/runner/translate-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 22, in enfr
    await ctx.channel.send(translation)
  File "/home/runner/translate-Bot/venv/lib/python3.8/site-packages/discord/abc.py", line 1065, in send
    data = await state.http.send_message(channel.id, content, tts=tts, embed=embed,
  File "/home/runner/translate-Bot/venv/lib/python3.8/site-packages/discord/http.py", line 254, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/translate-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/home/runner/translate-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/runner/translate-Bot/venv/lib/python3.8/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: HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message

It worked a few days ago and now it doesn't:( There is one other command, but it doesn't don't affect this one so I removed it from the code to clear it up a bit. I get that error when I try to run the command !enfr hello

I don't know if this is the correct place to ask, my last questions were disliked by some people.

The traceback specifies the error is Cannot send an empty message . Specifically:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message

Looking at it more closely you can find the line of your file where the Exception is being thrown.

File "main.py", line 22, in enfr await ctx.channel.send(translation)

From these it seems quite clear that translation is None or an empty string, and that the function ctx.channel.send throws an Exception when that is the case.

Now try to figure out why translation is empty by reviewing how you're creating it in the first place, and account for these edge-cases.

Note: Reading traceback logs might seem daunting at first, but they're quite informative (at worst showing you where in your code the issue is). Don't panic and just go through them slowly:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM