Reputation: 1
Hey everyone I want to delete all messages from a specific text channel I use this method
@client.event
async def on_message(message):
for guild in client.guilds:
for channel in guild.text_channels:
if channel.id == 818522056261369906:
await message.delete()
it works but it delete all text channels messages not just that text channel with the id above What is the problem
Upvotes: 0
Views: 82
Reputation: 103
it works but it delete all text channels messages not just that text channel with the id above What is the problem
You're deleting the message upon checking if a certain channel exists in your bot (i.e by checking if that channel exists in any of all the guilds your bot is in). You're not checking if the message's channel's id matches the provided ID or not. That's why it only checks if that specific channel exists and continues to delete the message regardless of weather they're in that specific channel or not.
for channel in guild.text_channels:
# you're checking if a certain channel in guild's
# text_channel matches the ID or not
if channel.id == 818522056261369906:
await message.delete()
But you ought to check if your message's channel's id matches the ID or not. You can do so by message.channel.id == 818522056261369906
. So your code should look something like this
if message.channel.id == 818522056261369906:
await message.delete()
Upvotes: 1
Reputation: 2346
The simplest way to do this would be to include get_channel(id)
and await purge
if you want to continuously delete messages from a channel.
@client.event
async def on_message(message):
channel = client.get_channel(818522056261369906) # this gets the specific channel through id
await channel.purge()
await client.process_commands(message)
However, if you only want to purge it once in a given channel, you would use a @client.command
with similar code as above.
@client.command()
async def test(ctx):
channel = ctx.channel # getting the current channel
await channel.purge()
Edit: Don't forget to add your await client.process_commands(message)
if you're using both the commands extension and the on_message
event!
Upvotes: 0