【问题标题】:How to stop a command during execution discord.py如何在执行期间停止命令 discord.py
【发布时间】:2020-09-09 08:12:06
【问题描述】:
@client.command()
async def spam(ctx):
  while True:
    time.sleep(2)
    await ctx.send("spam")
    @client.event
    async def on_message(message):
      if message.content == "!stop":
        # stop spamming 

我希望代码停止垃圾邮件,但我不确定如何。

【问题讨论】:

    标签: python bots discord


    【解决方案1】:

    使用wait_for试试这个例子:

    @client.command()
    async def spam(ctx):
    
        def check(m):
            return m.author == ctx.author and m.channel == ctx.channel
    
        stopped = False
        await ctx.send("spam")
        while not stopped:
            try:
                message = await client.wait_for("message", check=check, timeout=2)
                stopped = True if message.content.lower() == "!stop" else False
            except asyncio.TimeoutError: # make sure you've imported asyncio
                await ctx.send("spam")
        await ctx.send("spam finished!")
    

    参考资料:

    【讨论】:

    • 这行得通,谢谢 - 我是不和谐机器人的新手,在尝试之前可能应该多读一点,谢谢
    • @thepostyboi 不用担心,很高兴你能成功。如果您不介意,请accept the answer,因为它可以帮助其他正在寻找解决方案的人!祝你机器人的其余部分好运:)
    • 我接受了答案,对不起,我是这个网站的新手
    • @thepostyboi 没问题,希望你在这里过得舒服:^) 如果你还没有,你可以查看tour!编码愉快!
    猜你喜欢
    • 2013-03-09
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2020-12-31
    • 2021-01-18
    • 2015-08-02
    • 1970-01-01
    • 2021-11-17
    相关资源
    最近更新 更多