简体   繁体   中英

How do I delete the messages a discord bot sent after a time interval? (discord.py)

I'm making a discord bot using python that will send certain messages every few seconds. So it doesn't clutter the channel, I want it to delete the messages it last sent in the beggining of the while loop, to replace the messages with the new ones.

I don't know how to do this and any help would be greatly appreciated:)

@bot.command()
async def start(ctx):
    await ctx.send("Bot Started.")
    global bot_status
    bot_status = "running"
    while bot_status == "running":
        if bot_status == "stopped":
            break

        time.sleep(10)

        #delete
        #delete
        #delete

        await ctx.send("test")
        await ctx.send("test")
        await ctx.send("test")

.send() function has a parameter named delete_after . You can use it to delete the messages after a specific time. Also, instead of using strings, you can use booleans.

@bot.command()
async def start(ctx):
    await ctx.send("Bot Started.")
    global bot_status
    bot_status = True
    while bot_status == True:
        if bot_status == False:
            break
        await ctx.send("test", delete_after=11)
        await ctx.send("test", delete_after=11)
        await ctx.send("test", delete_after=11)
        await asyncio.sleep(10)

Or, you can use Message.delete . For that, you have to assign the sent messages to variables.

@bot.command()
async def start(ctx):
    await ctx.send("Bot Started.")
    global bot_status
    bot_status = True
    while bot_status == True:
        if bot_status == False:
            break
        msg1 = await ctx.send("test")
        msg2 = await ctx.send("test")
        msg3 = await ctx.send("test")
        await asycnio.sleep(10)
        await msg1.delete()
        await msg2.delete()
        await msg3.delete()

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