【问题标题】:python discord bot sleep to stop people spammingpython discord bot sleep 阻止人们发送垃圾邮件
【发布时间】:2022-02-12 17:49:50
【问题描述】:
尝试让机器人在再次回复之前休眠 5 秒。但我可以继续发送垃圾邮件“关闭”和机器人回复
import os
import random
import discord
import time
keywords3 = ['down']
@client.event
async def on_message(message):
global cooldown
if message.author == client.user:
return
messC = message.content.lower()
if any(word in messC for word in keywords3):
await message.channel.send('hello')
await asyncio.sleep(5)
`
【问题讨论】:
标签:
python
time
async-await
discord
【解决方案1】:
我不熟悉 discord api,但这应该可以满足您的需求
cooling_down = False
eventually_cool_up_task = None
async def eventually_cool_up(wait):
global cooling_down
await asyncio.sleep(wait)
cooling_down = False
@client.event
async def on_message(message):
global cooling_down, eventually_cool_up_task
if message.author == client.user:
return
messC = message.content.lower()
if any(word in messC for word in keywords3):
if not cooling_down:
cooling_down = True
await message.channel.send('hello')
eventually_cool_up_task = asyncio.create_task(eventually_cool_up(5))