Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

400 Bad Request (error code: 50006): Cannot send an empty message #8344

Closed
3 tasks done
TIBI4 opened this issue Aug 16, 2022 · 8 comments
Closed
3 tasks done

400 Bad Request (error code: 50006): Cannot send an empty message #8344

TIBI4 opened this issue Aug 16, 2022 · 8 comments
Labels
invalid This is not right.

Comments

@TIBI4
Copy link

TIBI4 commented Aug 16, 2022

Summary

Sometimes, when I send messages to channels the bot gets that error, even if the message is not empty.

Reproduction Steps

Create an embed, and send it. This happens sometimes.

(THE REPRODUCIBLE CODE, DOESN'T REPRODUCE THE ERROR ALWAYS)

Minimal Reproducible Code

import discord
from discord import app_commands
import asyncio

SELECTED_BOT_TOKEN = "???"

#Needed intents
class aClient(discord.Client):
    def __init__(self) -> None:
        super().__init__(intents=discord.Intents.default())
        self.synced = False

    async def on_ready(self):
        await self.wait_until_ready()
        await tree.sync()
        
client = aClient()
tree = app_commands.CommandTree(client)

@tree.command(name="test",description="TEST!")
@app_commands.describe(text="TEST...")
async def fixTranslation(interaction: discord.Interaction, text:str):
    await send(interaction.channel,interaction.user)
    
async def send(configChannel,member):
    #Variables
    badgeName = "BADGE_NAME"
    configDoPing = True
    pingText = "<@442706548242907148> ! Yeni bir rozet aldınız!"
    description = """<@442706548242907148> **fifa 22** aldı ve şimdi 1 rozeti var!

 Bu rozet 6965 üyeye aittir."""
    emoji = None
    #Embed
    e = discord.Embed(title="TITLE_HERE", description=description)
    #Add image
    imageUrl = "https://dibot.tibi4.com/badges/Server%20Badges/B_68068.png?t=1658908284"
    e.set_thumbnail(url=imageUrl)
    #Add footer
    if emoji is not None:
        e.set_footer(text=f"This badge has an emoji! ⟶ {emoji}")
    #Send (and edit if needed)
    try:
        print(f"Sent announcement: {badgeName} (PingText: {pingText is not None})")
        announcementMessage = await configChannel.send(content=pingText,embed=e)
        if configDoPing:
            await asyncio.sleep(5)
            await announcementMessage.edit(content=None,embed=e)
    except Exception as excep:
        print(f"""Variables:
    configDoPing = {configDoPing}
    pingText = {pingText}
    description = {description}
    imageUrl = {imageUrl}
    emoji = {emoji}
    member = {member}
    member.guild.id = {member.guild.id}
    member.guild.name = {member.guild.name}
    """)
        print(f"VARIABLES ABOVE!\n{excep}")
        

client.run(SELECTED_BOT_TOKEN)

Expected Results

The message to be sent successfully.

Actual Results

Error happens, and message not sent.

Intents

discord.Intents.default() (and) intents.presences = True (and) intents.members = True (and) intents.message_content = False

System Information

python3.9 -m discord -v

  • Python v3.9.4-final
  • discord.py v2.0.0-alpha
    • discord.py pkg_resources: v2.0.0a4448+g13c725f1
  • aiohttp v3.8.1
  • system info: Linux 4.9.0-8-amd64 Fix issue with author changing type #1 SMP Debian 4.9.144-3.1 (2019-02-19)

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

The bot uses that function (send) many times (at least 1 per minute), but this problem happens only sometimes. I can't reproduce the problem always. It happens every 30-60mins.

The configChannel is a TextChannel, and the member is a Member.

@TIBI4 TIBI4 added the unconfirmed bug A bug report that needs triaging label Aug 16, 2022
@Rapptz
Copy link
Owner

Rapptz commented Aug 16, 2022

Sorry. This issue is not actionable as-is.

The HTTPException comes from the request to Discord failing and in this case would most likely denote an issue with your code. For example, things being None or a partially filled embed. There isn't really enough information here to debug this for you, and this issue tracker is not really the place to do so as it is reserved for library bugs.

@Rapptz Rapptz closed this as not planned Won't fix, can't repro, duplicate, stale Aug 16, 2022
@Rapptz Rapptz added invalid This is not right. and removed unconfirmed bug A bug report that needs triaging labels Aug 16, 2022
@TIBI4
Copy link
Author

TIBI4 commented Aug 16, 2022

@Rapptz But for example, the embed above, when the exception comes, prints (there are two prints on the catched exception):

First prints:

Variables:
configDoPing = True
pingText = <@442706548242907148> ! Yeni bir rozet aldınız!
description = <@442706548242907148> fifa 22 aldı ve şimdi 1 rozeti var!

 Bu rozet 6965 üyeye aittir.
imageUrl = https://dibot.tibi4.com/badges/Server%20Badges/B_68068.png?t=1658908284
emoji = None
member = İsmailYILMAZ#3083
member.guild.id = 790749034385637485
member.guild.name = Combat Arms Community

And then, the next print, prints:

VARIABLES ABOVE!
400 Bad Request (error code: 50006): Cannot send an empty message

Meaning that imageUrl and description are not None, and the embed is not filled with any None values. Also pingText is not None.

Also, I was debugging printing e.to_dict(), and it didn't print any None in the dict. (I can modify the code so you can check).

Any suggestion on how to debug it beside this? What information can I provide?

Thanks!

@Rapptz
Copy link
Owner

Rapptz commented Aug 16, 2022

You're swallowing the exception on two different endpoints which send the same error, you forgot to check whether e actually contained any text (and no, that doesn't mean None, the dict will never contain None values).

@TIBI4
Copy link
Author

TIBI4 commented Aug 16, 2022

I checked e before with the e.to_dict() and all fields were filled with text. I have no proof of that now, but I'm 90% sure.

I will check again with this code on the exception (when it comes again, as it is totally random):

print(f"""ERROR:
Embed as dict:
{e.to_dict()}

Variables:
configDoPing = {configDoPing}
pingText = {pingText}
description = {description}
imageUrl = {imageUrl}
emoji = {emoji}
member = {member}
member.guild.id = {member.guild.id}
member.guild.name = {member.guild.name}

Exception:
{excep}"""

I didn't understood when you said:

"You're swallowing the exception on two different endpoints which send the same error."

Am I not understanding something, or missing something? Sorry for that.

Thanks!

@Rapptz
Copy link
Owner

Rapptz commented Aug 16, 2022

Both announcementMessage = await configChannel.send(content=pingText,embed=e) and await announcementMessage.edit(content=None,embed=e) will raise that exception but you're catching both of them in the same block so it's hard to tell which one is actually failing.

There's also probably a chance that your variables are being modified between the sleep call and they're globals or something. Either way, I can't really offer much help since this seems to be an application error.

@TIBI4
Copy link
Author

TIBI4 commented Aug 16, 2022

Sorry to keep bothering you...

There's also probably a chance that your variables are being modified between the sleep call and they're globals or something.

The function is not using global variables.

Both announcementMessage = await configChannel.send(content=pingText,embed=e) and await announcementMessage.edit(content=None,embed=e) will raise that exception

Ohhh, well. I thought that only send could raise it. I checked that, and it is in the edit. (See the traceback below).

So... I hit the error again.

I can see that e was successfully sent at send but not in edit (with no changes). I'm not changing values for e anywhere else (as it is not global), and e.to_dict() seems unchanged too (see the print below), so sending e should be working. Am I wrong?

To test it, I changed the try part of the code to:
(You can ignore discordPrint, as it is just a webhook to send the error messages to a channel)

#(...)
    #Send (and edit if needed)
    raisedAtEdit = False
    try:
        print(f"Sent announcement: {badgeName} (PingText: {pingText is not None})")
        announcementMessage = await configChannel.send(content=pingText,embed=e)
        if configDoPing:
            await asyncio.sleep(5)
            raisedAtEdit = True
            await announcementMessage.edit(content=None,embed=e)
    except Exception as excep:
        discordPrint("on_alert",f"""ERROR:
Embed as dict:
{e.to_dict()}

Variables:
raisedAtEdit = {raisedAtEdit}
configDoPing = {configDoPing}
pingText = {pingText}
description = {description}
imageUrl = {imageUrl}
emoji = {emoji}
member = {member}
member.guild.id = {member.guild.id}
member.guild.name = {member.guild.name}

__VARIABLES ABOVE!__
{excep}

Traceback:
{traceback.format_exc()}""")

Printing:

ERROR:
Embed as dict:
{'thumbnail': {'url': 'https://dibot.tibi4.com/badges/Server%20Badges/B_14.png?t=1658908284%27%7D, 'color': 15959600, 'type': 'rich', 'description': '<@604179873660928000> obtained Spotify, and now has 1 badges!\n\nThis badge is owned by 205868 members.', 'title': '! † 𝐽𝑂𝑁𝐴 ϟ obtained a badge!'}

Variables:
raisedAtEdit = True
configDoPing = True
pingText = Hey, <@604179873660928000>! You received a new badge!
description = <@604179873660928000> obtained Spotify, and now has 1 badges!

This badge is owned by 205868 members.
imageUrl = https://dibot.tibi4.com/badges/Server%20Badges/B_14.png?t=1658908284
emoji = None
member = ! † 𝐽𝑂𝑁𝐴 ϟ#6460
member.guild.id = 824441321754198067
member.guild.name = ERRIE ISLAND PVP/ MERCADO EVENTOS

VARIABLES ABOVE!
400 Bad Request (error code: 50006): Cannot send an empty message

Traceback:
Traceback (most recent call last):
File "/home/DiBot/Utilities.py", line 554, in announceBadge
await announcementMessage.edit(content=None,embed=e)
File "/usr/local/lib/python3.9/site-packages/discord/message.py", line 2088, in edit
data = await self._state.http.edit_message(self.channel.id, self.id, params=params)
File "/usr/local/lib/python3.9/site-packages/discord/http.py", line 715, in request
raise HTTPException(response, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message

@z03h
Copy link
Contributor

z03h commented Aug 16, 2022

Your bot is probably missing embed_links permission in that channel.
Discord does not return an error if you try to send an embed without the permission, instead the embed is not there. Your message edit removes content and without the embed it's an empty message.

@TIBI4
Copy link
Author

TIBI4 commented Aug 16, 2022

Your bot is probably missing embed_links permission in that channel. Discord does not return an error if you try to send an embed without the permission, instead the embed is not there. Your message edit removes content and without the embed it's an empty message.

It was that!

Thanks for all the help! To both of you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This is not right.
Projects
None yet
Development

No branches or pull requests

3 participants