@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
我希望代码停止发送垃圾邮件,但我不确定如何
使用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!")
参考:
Client.wait_for()
Client.wait_for()
Message.author