【问题标题】:Trying to get my discord bot in python to say the same message every 5 seconds试图让我的不和谐机器人在 python 中每 5 秒说一次相同的消息
【发布时间】:2020-04-25 10:16:12
【问题描述】:
我正在为 dank memer 做一些“hack”。我已经制作了自己的机器人。我对机器人的目标是让它每隔 5 秒就一直说“请搜索”。最终,我会发出“请给 100”的命令来获取硬币。它没有出现在不和谐中,而是出现在我的应用程序中。
@client.command()
async def go(ctx):
def printit():
threading.Timer(5.0, printit).start()
await ctx.send("pls search")
printit()
【问题讨论】:
标签:
python
discord
discord.py
【解决方案1】:
这将连续发送消息,但您会受到速率限制。这通过在发送消息的事件循环中调度一个任务,然后取消它来实现。
import asyncio
@bot.command()
async def comm(ctx):
async def f():
while True:
# await asyncio.sleep(.5) # Control message speed
await ctx.send('Test')
await ctx.send("Start")
task = asyncio.create_task(f())
await asyncio.sleep(5)
await ctx.send("End")
task.cancel()
【解决方案2】:
你可以这样做:
import time
@bot.command()
async def start(ctx, number_of_times=5):
for i in range(number_of_times):
await ctx.send('pls search')
time.sleep(5)
# type in the user
await ctx.send('pls give <user> 100')