Reputation: 3
I have an issue about discord.py. I have a code for forwarding embed messages to another discord channel, but it gives me an error. How can I solve it?
Error:
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)
edit: i added two lines but still same error (discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message). can anyone share full code. am i wrong?
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)
Edit 1: sent message is "@deleted role"
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 discord.errors.HTTPException:
pass
print(f"Server: {serverName}, Channel: {channelName}")
client = MyClient() client.run(USER_DISCORD_TOKEN)
Upvotes: 0
Views: 808
Reputation: 354
Have you checked what the value of the variable message.embeds[0]
is? If message.embeds[0]
is None
(NoneType), then it is very possible that it is the reasoning behind your issue. Note these things:
Try see the value of message.embeds[0]
. Also, I noticed something as I was doing testing for you.
You can see that the first object is from await channel.send(embed=embed)
, but the second one is from await channel.send(message.embeds[0])
. Have you tried manually adding the arguments of the discord.Embed
object to another embed? Like:
await channel.send(
embed = discord.Embed(
title = message.embeds[0].title,
description = message.embeds[0].description,
# etc.
)
)
Upvotes: 0
Reputation: 2720
The "Cannot send an empty message" error happens because the bot tries to send a message with empty content and no embed (presumably because there was a message in the source channel that had neither, e.g. a message with just an image - those go to message.attachments
, not to message.content
)
The easiest way to get around this is to simply try: ... except
your await channeltosend.send(message.content, embed=message.embeds[0])
call and ignore/log the error, but you may want to copy any attachments to the target channel anyways, so perhaps you should check for attachments and copy them over instead. You can achieve that by using to_file()
on the attachment and then submit that to the file
-parameter of channeltosend.send
(or files
and a list of attachments if there are multiple) something like:
files = [await attachment.to_file() for attachment in message.attachments]
...
await channeltosend.send(message.content, embed=message.embeds[0], files=files)
Upvotes: 1