簡體   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