How to check if a channel has any messages? Discord.py

xXSkillexZ

So what I want to achieve right now is, to find and write a code so whenever I purge the channel, it will first check if it has any messages in it. If it does not then it will send an error. The only problem is that I do not know if it's possible for the client to first check if there are any messages on it. If anyone has any ideas or examples, I would be really happy to know!

Answer is this:

    @commands.command()
    async def clear(self, ctx, *, limit=100):
        await ctx.message.delete()
        channel = ctx.channel
        messages = await channel.history(limit=123).flatten()
        if not messages:
            await ctx.channel.send("I am really sorry but I can not purge an empty channel!")
            return
        else:
            try:
                await channel.purge(limit = limit)
                return
            except discord.Forbidden:
                return await ctx.channel.send('I do not have perms')

Kelo

You can use messages = await channel.history(limit=123).flatten() to get a list containing channel's messages. Limit is used to specify a maximum number of messages to read back.

You can check if that list is empty or not to check if there are messages in a channel.

API Reference: https://discordpy.readthedocs.io/en/latest/api.html?highlight=history#discord.TextChannel.history

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I use discord.py to send a greeting message in the System Messages Channel

From Dev

Discord Py 'NoneType' object has no attribute 'voice_channel'

From Dev

Discord.js - How to spam messages in a text channel?

From Dev

How to create a category and a channel using python discord.py

From Dev

Discord py how can I join a voice channel

From Dev

discord.py How to get logs from a DM channel

From Dev

Get the channel deletor discord py

From Dev

How to check if an Azure storage queue has messages

From Dev

How to check if an app has an access to Direct Messages?

From Dev

Discord.py bot.send_message() does not go to the channel it has been called at

From Dev

How do I move a user to a specific channel on discord using the discord.py API

From Dev

How to check if json array has any value?

From Dev

How to check if any attribute has changed on a model

From Dev

jQuery: How to check if the content has any URLs

From Dev

How to check if any attribute has changed on a model

From Dev

How to check if folder has any content in puppet

From Dev

How to check if a batch file has ANY parameters?

From Dev

How to check if json array has any value?

From Dev

How to check if XElement has any child nodes?

From Java

message.channel.id Discord PY

From Java

Cloning and Deleting a channel: Discord.py

From Dev

Discord.py bot leaving voice channel

From Dev

How can I make my Discord Bot delete all messages in a channel?

From Dev

How do i delete the previous messages that my discord bot sent in a specific channel?

From Dev

How can I make my Discord Bot delete all messages in a channel?

From Java

discord.py (rewrite) How do I make a command to a specific channel

From Java

discord.py (rewrite) How can I narrow a commands use ability to a single channel?

From Dev

how can i make my discord.py bot send a message i choose to a channel i choose?

From Dev

How to access which voice channel user writing command is in discord.py?

Related Related

  1. 1

    How do I use discord.py to send a greeting message in the System Messages Channel

  2. 2

    Discord Py 'NoneType' object has no attribute 'voice_channel'

  3. 3

    Discord.js - How to spam messages in a text channel?

  4. 4

    How to create a category and a channel using python discord.py

  5. 5

    Discord py how can I join a voice channel

  6. 6

    discord.py How to get logs from a DM channel

  7. 7

    Get the channel deletor discord py

  8. 8

    How to check if an Azure storage queue has messages

  9. 9

    How to check if an app has an access to Direct Messages?

  10. 10

    Discord.py bot.send_message() does not go to the channel it has been called at

  11. 11

    How do I move a user to a specific channel on discord using the discord.py API

  12. 12

    How to check if json array has any value?

  13. 13

    How to check if any attribute has changed on a model

  14. 14

    jQuery: How to check if the content has any URLs

  15. 15

    How to check if any attribute has changed on a model

  16. 16

    How to check if folder has any content in puppet

  17. 17

    How to check if a batch file has ANY parameters?

  18. 18

    How to check if json array has any value?

  19. 19

    How to check if XElement has any child nodes?

  20. 20

    message.channel.id Discord PY

  21. 21

    Cloning and Deleting a channel: Discord.py

  22. 22

    Discord.py bot leaving voice channel

  23. 23

    How can I make my Discord Bot delete all messages in a channel?

  24. 24

    How do i delete the previous messages that my discord bot sent in a specific channel?

  25. 25

    How can I make my Discord Bot delete all messages in a channel?

  26. 26

    discord.py (rewrite) How do I make a command to a specific channel

  27. 27

    discord.py (rewrite) How can I narrow a commands use ability to a single channel?

  28. 28

    how can i make my discord.py bot send a message i choose to a channel i choose?

  29. 29

    How to access which voice channel user writing command is in discord.py?

HotTag

Archive