![](/img/trans.png)
[英]Discord.py How can I make a bot delete messages after a specific amount of time
[英]How do I delete the messages a discord bot sent after a time interval? (discord.py)
我正在使用 python 制作一个 discord 机器人,它将每隔几秒发送一次特定消息。 所以它不会使通道混乱,我希望它删除它在 while 循环开始时最后发送的消息,用新消息替换消息。
我不知道该怎么做,任何帮助将不胜感激:)
@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 有一个名为delete_after
的参数。 您可以使用它在特定时间后删除消息。 此外,您可以使用布尔值而不是使用字符串。
@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)
或者,您可以使用Message.delete
。 为此,您必须将发送的消息分配给变量。
@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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.