【问题标题】:discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty messagediscord.errors.HTTPException:400 Bad Request(错误代码:50006):无法发送空消息
【发布时间】:2021-10-06 18:41:25
【问题描述】:

我正在尝试测试我是否可以将所有发送到 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

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

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    好吧,我认为正在发生的事情是当你写 :!test !test 在另一个通道中触发,这反过来又会触发嵌入通过另一个通道,因为你有 if message.content.startswith('!test') 这不是行特定渠道。

    但是,发生的问题是在发送嵌入时调用 on_message 事件函数。嵌入没有内容,因此当您尝试在 await test_channel.send(message.content) 行中发送此内容时,会发生错误,因为 message.content 为空(因为嵌入不是内容)。

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

    否则您应该阅读此post 以了解如何从消息中获取嵌入信息(简而言之为embed_content_in_dict = message.embeds[0].to_dict()

    希望这是有道理的:)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      • 2023-02-24
      • 2017-08-10
      • 2020-12-20
      相关资源
      最近更新 更多