discord.errors.HTTPException:400 错误请求(错误代码:50006):无法发送空消息

时间:2021-07-31 17:01:13

标签: python discord discord.py

我试图测试我是否可以基本上复制发送到 test_copy_channel 频道的所有消息并将它们粘贴到 test_paste_channel
尽管机器人正在执行命令并正确记录嵌入,但我不断收到错误消息。

这是我正在使用的代码:

import discord
import os
from discord.ext import commands


intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=',', intents=intents)


@bot.event
async def on_ready():
  global test_paste_channel, test_copy_channel
  test_paste_channel = bot.get_channel(868816978293452841)
  test_copy_channel = bot.get_channel(808734570283139162)
  print('bot is ready')


@bot.event
async def on_message(message):
  # if message.author == bot.user:
  #   return

  if message.channel == test_copy_channel:
    await test_paste_channel.send(message.content)
    print(message.channel)

  if message.content.startswith('!test'):
    embed_var = discord.Embed(
      title= '''title''', 
      description= '''description''', 
    color= discord.Color.red()
      )
    
    embed_var.set_footer(text='footer')
    await message.channel.send(embed=embed_var)


bot.run(os.getenv('TOKEN'))

所以我要做的是将 !test 发送到 test_copy_channel,以便机器人发送嵌入然后尝试复制我的消息和嵌入
我的消息通过得很好,但是当机器人尝试复制嵌入时,我收到此错误:

Ignoring exception in on_message
Traceback (most recent call last):
⠀⠀File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
⠀⠀⠀⠀await coro(*args, **kwargs)
⠀⠀File "main.py", line 25, in on_message
⠀⠀⠀⠀await test_channel.send(message.content)
⠀⠀File "/opt/virtualenvs/python3/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 "/opt/virtualenvs/python3/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

它似乎并没有阻止命令的执行,并且代码似乎可以正常工作。
据我了解,当它尝试复制机器人发送的嵌入消息时会触发该错误。
我只想知道为什么会触发此错误。

1 个答案:

答案 0 :(得分:1)

好吧,我认为发生的事情是当你写:!test !test 在另一个通道中触发,反过来触发嵌入通过另一个通道,因为你有 {{ 1}} 不是特定于频道的。

然而,发生的问题是在发送嵌入时调用 if message.content.startswith('!test') 事件函数。嵌入没有内容,因此当您尝试在 on_message 行中发送此内容时,会发生错误,因为消息是空的(因为嵌入不是内容)。

解决此问题的一种作弊方法是在 await test_channel.send(message.content) 上方添加行 if message.content:,因为由于 await test_channel.send(message.content) 在另一个频道中发送,因此嵌入无论如何都会发送。

否则,您应该阅读此 post 以了解如何从消息中获取嵌入信息(简而言之,它的 !test

希望这是有道理的:)