如何使某个函数永远重复



所以我想做一个mps系统(每秒钱),这是我得到的:

class Test(commands.Cog):
def __init__(self,client):
self.client = client
self.money.start()

@tasks.loop(seconds=1)
async def money(self):
users = await self.get_bank_data()
for id in ids:  
money = users[str(id)]["wallet"]
cps = 1
money += cps
await self.update_bank(id, money ,"wallet")

但是它似乎在for id in ids:上卡住了,但是我运行了@client.event,它打印了id。

@client.event

@client.event
async def on_ready():
print("Ready!")
for cog in cogs:
print(cog,"working")
client.load_extension(cog)
for id in ids:
print(id)
return

输出:my id

我能想到的最简单的方法是使用discord.ext.tasks

首先实现一个全局变量来保存要更新的用户id。按你喜欢的做,把它们设置在数据库或其他地方。

然后

from discord.ext import tasks
@tasks.loop(seconds=1)
async def money(self):
for user_id in list_of_users:  # define this somewhere
monei = users[str(user_id)]["wallet"]
cps = 1
monei += cps
await self.update_bank(user_id, monei ,"wallet")  # update this function to use user_id or use bot.get_user or guild.get_member to retreive the appropiate object.

使用任务。循环是设置重复功能的最简单方法。注意,循环没有上下文。

下面是相应的文档:tasks.loop

最新更新