繁体   English   中英

有人可以帮我找出这段代码中的错误吗?

[英]Could someone help me find the error in this 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)

这是我得到的错误:

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

它在几天前工作,现在它没有:(还有一个命令,但它不影响这个命令所以我从代码中删除它以清除它一点。当我得到那个错误时我尝试运行命令 !enfr hello

我不知道这是不是该问的地方,有些人不喜欢我最后的问题。

回溯指定错误是Cannot send an empty message 具体来说:

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

更仔细地查看它,您可以找到抛出异常的文件行。

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

从这些中可以很清楚地看出translation是 None 或空字符串,并且 function ctx.channel.send在这种情况下会抛出异常。

现在尝试通过首先检查您是如何创建翻译来弄清楚为什么translation是空的,并考虑这些边缘情况。

注意:一开始阅读回溯日志可能让人望而生畏,但它们提供的信息非常丰富(最坏的情况是向您展示代码中的问题所在)。 不要惊慌,慢慢地通过它们 go :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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