问题描述
我在GitHub上找到了此代码,并试图使其在当前时间每分钟更新一次discord上的语音通道。不幸的是,无论我重新启动代码多少次,此代码似乎仅每10分钟工作一次,然后在每分钟更新一次。不是Python专家,所以不能完全确定所有语法,但是如果有人可以引导我了解所有语法错误,将不胜感激。如果相关的话,我也将其托管在repl.it上。
import discord
import asyncio
import datetime
client = discord.Client()
distoken = "xxxxxx"
# These must all be Voice Channels
timechannel = xxxxxx
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
while True:
Now = datetime.datetime.Now()
await client.get_channel(timechannel).edit(name=f"{Now.hour}:{Now.minute} (BST)") # The channel gets changed here
await asyncio.sleep(60)
client.run(distoken)
解决方法
要每隔X秒/分钟/小时执行某项操作,您应该使用内置的tasks,它是为此目的而设计的。任务的工作方式如下:
from discord.ext import tasks
@tasks.loop(seconds=5)
async def my_loop():
# do_something_here
my_loop.start()