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')
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.
Comments