首页 > 解决方案 > 在 discord.py 中停止机器人命令

问题描述

所以我写了一个这样的垃圾邮件机器人:

@bot.command()
async def start (ctx):
    while True:
        await ctx.send("Pls use correct channels for discussion")
        await asyncio.sleep(300)

当我输入 $start 时,函数启动。如何制作像 $stop 这样的新命令来阻止垃圾邮件?

标签: discord.py

解决方案


一个简单的方法是设置一个变量,如果垃圾邮件已经开始,什么时候停止。当您运行 start 命令时,spam 变量已经为 true,并且会运行,因为 if 语句继续执行 while 循环。运行 stop 命令时,它只是将 spam 变量设置为 false 并且不会继续运行 spam

spam = 'true'

@bot.command()
async def start (ctx):
    if spam == 'true':
        while true:
            await ctx.send("Pls use correct channels for discussion")
            await asyncio.sleep(300)


@bot.command()
async def stop (ctx):
    global spam
    spam = 'false'
    await ctx.send('spam stopped')

推荐阅读