简体   繁体   中英

How To Create A Purge Command With Discord.py

I can't figure out how to create the purge command in discord.py. This is what I am currently using it creates no errors in the code but the bot doesn't respond to the command.

client = discord.Client()

@client.event
async def on_message(message):
if message.content.startswith('!purge'):
    tmp = await client.send_message(message.channel, 'Clearing messages...')
    async for msg in client.logs_from(message.channel):
        await client.delete_message(msg)

By the looks of it you're using the async branch (please switch to rewrite ) of discord.py

For one doing the whole command invocation in on_message thing is not, while it works, a good way of doing it. Try out the commands extension !

Secondly there is already a method for this in async see here

Thirdly the reason why your code isn't working or why it doesn't look like it's working is because you are using client.logs_from with no limit specified so it's downloading every message in that channel and this will take a while to do before it executes the code below it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM