停止命令-PYTHON DISCORD

问题描述 投票:1回答:1
@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个回答
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!")

参考:

© www.soinside.com 2019 - 2024. All rights reserved.