dolfies / discord.py-self

A fork of the popular discord.py for user accounts.
https://discordpy-self.rtfd.io/en/latest/
MIT License
684 stars 162 forks source link

Sending a message to a channel with cooldown enabled doesn't work nor does it raise an error #172

Closed theAbdoSabbagh closed 2 years ago

theAbdoSabbagh commented 2 years ago

Summary

So basically, if you try bot.fetch_channel() then try to send a message to that channel, WHILE from the client you can't send a message because of the cooldown, it's not gonna work, and it's not gonna raise an error

Reproduction Steps

I was trying to automate the process of sending messages to multiple channels, their IDs were stored as integers in a list. I looped over the list and tried fetching the channel and sending a message to it, and it happened that the first ID in the list was an ID of a channel that I had already sent a message to before, but I wanted to use it as a test, basically, if I put it in a try/except block, and I wanted to send a message to the channel, and print an error message if I faced an error, but it didn't even print, and the message was NOT sent. I removed it out of the try/except block, nothing changed, no error was raised. All the indentation was valid, I had no errors in the code and everything was defined as well.

Code

example_list = [channel_id_1, channel_id_2]
# channel_id_1 is an ID of a channel that you cannot send a message to in the client due to cooldown
# channel_id_2 is an ID of a channel that you can send a message to
# Results are the same inside of commands and events
for ID in example_list:
  try: # Doesn't matter if it was inside or outside of a try/except block
    channel = await bot.fetch_channel(ID)
  except:
    # something
    pass
  else:
    try: # Doesn't matter if it was inside or outside of a try/except block
      await channel.send("any message") # Doing await ctx.send(channel) works, only channel.send doesn't work and it doesn't raise an exception/error
    except:
      # something
      pass
    # now it'll stop the whole for loop, not even continue to the next ID in the list.

Expected Results

Send a message and if it failed, raise an exception.

Actual Results

Weirdly stops and doesn't raise an exception.

System Information

Checklist

Additional Information

No response

dolfies commented 2 years ago

That's the library's rate-limit handling handling the 429.

Avoid sending messages without abiding my slow mode because: a) This happens b) It's suspicious

theAbdoSabbagh commented 2 years ago

Shouldn't there be something like a print statement that makes it clear for people or something? Also what's a proper way of achieving my goal if that's what the library is going to do?

TheOnlyWayUp commented 2 years ago

I suggest you store your channels as a list of channel objects instead of a list of IDs which you fetch every time, maybe an option to raise_for_status in channel.send?

dolfies commented 2 years ago

Shouldn't there be something like a print statement that makes it clear for people or something? Also what's a proper way of achieving my goal if that's what the library is going to do?

Putting a print statement in a library isn't proper. When a ratelimit is hit, it's logged.

The library is respecting what Discord asks it to do, and sleeps until the ratelimit passes before retrying sending the message.

If you want to avoid this, fetch the channel's message history, find your last message, and calculate the time passed to figure out when you can send a message next.

TheOnlyWayUp commented 2 years ago

@Gltxched close the issue if there are no more questions please.

TheOnlyWayUp commented 2 years ago

@dolfies why is this issue still open, it's usually common practice to close issues if there's nothing more to add. Comments can still be added to close issues, and reopened if it was closed by the author themselves.