繁体   English   中英

我如何在不重启机器人的情况下停止这个 discord 机器人命令?

[英]How i can stop this discord bot command wihout restart the bot?

我如何在不重启机器人的情况下停止这个 discord 机器人命令?

@bot.command()
async def spam(ctx, *args):
    response = ""
    for arg in args:
        response = response + " " + arg
    while True:
        await ctx.channel.send(response)
        time.sleep(1)

你可以简单地使用一个变量

bot.some_var = True

while bot.some_var:
    await ctx.send(response)
    if 'some condition met':
        loop = False

如果你想在命令中停止循环:

bot.some_var = True

@bot.command()
async def spam(ctx, *args):
    response = ' '.join(args)

    while bot.some_var:
        await ctx.send(response)
        await asyncio.sleep(1)


@bot.command()
async def stop(ctx):
   bot.some_var = False

还有两件事:

  1. 您可以简单地str.join(list)而不是您制作的那个奇怪的 for 循环
  2. 使用asyncio.sleep而不是time.sleep这样你的代码就不会被阻塞

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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