【问题标题】:discord.py error when forwarding embeds to another channel将嵌入转发到另一个频道时出现 discord.py 错误
【发布时间】:2022-04-03 13:40:35
【问题描述】:

我有一个关于 discord.py 的问题。我有一个将嵌入消息转发到另一个不和谐频道的代码,但它给了我一个错误。我该如何解决?

错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Roaming\Python\Python310\site-packages\discord\client.py", line 301, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\USER\Desktop\newBodyguard\d2d.py", line 27, in on_message
    await channeltosend.send(message.content, embed=message.embeds[0])
  File "C:\Users\USER\AppData\Roaming\Python\Python310\site-packages\discord\abc.py", line 1061, in send
    data = await state.http.send_message(channel.id, content, tts=tts, embed=embed,
  File "C:\Users\USER\AppData\Roaming\Python\Python310\site-packages\discord\http.py", line 302, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message
import discord
from discord import channel

USER_DISCORD_TOKEN = 'u3VulQw33THPGbPlx_aUHC.............'
channel1id = 12844415 #source channel 
channel2id = 71038106 #target channel

async def on_ready():
    print("Copier is ready")

class MyClient(discord.Client):
   
        
    async def on_ready(self):
        print('Logged on as', self.user)
    
    async def on_message(self, message):
        try:
            serverName = message.guild.name
            channelId = message.channel.id
            channelName = message.channel.name
        except AttributeError:
            pass
        print(f"Server: {serverName}, Channel: {channelName}")
        if message.channel.id == channel1id:
            channeltosend = client.get_channel(channel2id)
            await channeltosend.send(message.content, embed=message.embeds[0])
            print(message)
            print('****************')
            print(message.embeds)
       
               
client = MyClient()
client.run(USER_DISCORD_TOKEN)

编辑:我添加了两行但仍然是相同的错误(discord.errors.HTTPException:400 Bad Request(错误代码:50006):无法发送空消息)。任何人都可以分享完整的代码。我错了吗?

import discord
from discord import channel

USER_DISCORD_TOKEN = 'TOKEN HERE'
channel1id = 823473 #source
channel2id = 910981 #dest

async def on_ready():
    print("Bot is ready")

class MyClient(discord.Client):
   
        
    async def on_ready(self):
        print('Logged on as', self.user)
    
    async def on_message(self, message):
    try:
        serverName = message.guild.name
        channelId = message.channel.id
        channelName = message.channel.name
        print(f"Server: {serverName}, Channel: {channelName}")
        if message.channel.id == channel1id:
            channeltosend = client.get_channel(channel2id)
            await channeltosend.send(message.content, embed=message.embeds[0])
            print(message)
            print('****************')
            print(message.embeds)
    except AttributeError:
        pass
    print(f"Server: {serverName}, Channel: {channelName}")
       
               
client = MyClient()
client.run(USER_DISCORD_TOKEN)

编辑 1:发送的消息是“@deleted 角色”

async def on_ready(): print("机器人准备好了")

类 MyClient(discord.Client):

async def on_ready(self):
    print('Logged on as', self.user)

async def on_message(self, message):
    try:
        serverName = message.guild.name
        channelId = message.channel.id
        channelName = message.channel.name
        print(f"Server: {serverName}, Channel: {channelName}")
        if message.channel.id == channel1id:
            channeltosend = client.get_channel(channel2id)
            await channeltosend.send(message.content, embed=message.embeds[0])
            print(message)
            print('****************')
            print(message.embeds)
    except discord.errors.HTTPException:
        pass
    print(f"Server: {serverName}, Channel: {channelName}")
   
           

客户 = 我的客户() client.run(USER_DISCORD_TOKEN)

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    “无法发送空消息”错误的发生是因为机器人试图发送一条内容为空且没有嵌入的消息(可能是因为源频道中有一条消息两者都没有,例如一条只有图像的消息 -那些去message.attachments,而不是message.content


    解决此问题的最简单方法是简单地 try: ... except 调用您的 await channeltosend.send(message.content, embed=message.embeds[0]) 并忽略/记录错误,但您可能仍希望将任何附件复制到目标频道,因此也许您应该检查附件并而是将它们复制过来。您可以通过在附件上使用to_file() 来实现这一点,然后将其提交到channeltosend.sendfile 参数(或files 和附件列表,如果有多个)类似:

    files = [await attachment.to_file() for attachment in message.attachments]
    ...
    await channeltosend.send(message.content, embed=message.embeds[0], files=files)
    

    【讨论】:

    • 不适合我。我只想将所有消息(文本和频道中的嵌入消息)从一个频道复制到另一个频道。如果你喜欢这个代码,可以分享一下吗?
    • 您能否详细说明“不适合我”?你改变了什么?发生了什么?
    • python 代码已编辑。但仍然错误
    • 我认为这对于贴纸和类似物品仍然会失败,尽管我没有测试过。只需将您的 channeltosend.send 消息包装在 try: ... except: 语句中并处理 except 子句中的空消息
    • 我改了但还是出错。
    【解决方案2】:

    你检查过变量message.embeds[0] 的值是多少吗?如果message.embeds[0]None (NoneType),那么很可能是您的问题背后的原因。请注意以下几点:

    1. 如果您要发送嵌入,则无需发送消息
    2. 当您发送嵌入时,您至少需要一个标题或描述。这是绝对最小值。

    尝试查看message.embeds[0] 的值。另外,在为您进行测试时,我注意到了一些事情。

    可以看到第一个对象来自await channel.send(embed=embed),而第二个对象来自await channel.send(message.embeds[0])。您是否尝试过手动将 discord.Embed 对象的参数添加到另一个嵌入?喜欢:

    await channel.send(
        embed = discord.Embed(
            title = message.embeds[0].title,
            description = message.embeds[0].description,
            # etc.
        )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 2021-07-31
      • 2021-04-04
      • 2021-02-20
      相关资源
      最近更新 更多