有没有一种方法可以让我得到这个代码来每分钟可靠地更新一个不和谐的频道



我在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秒/分钟/小时做一件事,您应该使用内置任务,这些任务正是为此目的而设计的。任务工作方式如下:

from discord.ext import tasks
@tasks.loop(seconds=5)
async def my_loop():
# do_something_here
my_loop.start()

最新更新