我如何停止这个不和谐的机器人命令而不重新启动机器人?

问题描述 投票:0回答:1

我如何停止这个不和谐的机器人命令而不重新启动机器人?

@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)
python python-3.x discord.py
1个回答
1
投票

您可以简单地使用变量

bot.some_var = True

while bot.some_var:
    await ctx.send(response)
    if 'some condition met':
        bot.some_var = 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
    这样你的代码就不会被阻止
© www.soinside.com 2019 - 2024. All rights reserved.