【问题标题】:Save users list in main loop [Discord bot]在主循环中保存用户列表 [Discord bot]
【发布时间】:2021-08-13 21:01:58
【问题描述】:
您需要调用该函数来保存特定时间的用户列表。
例如:
def save(array):
with open('users.json', 'w') as file:
json.dump(array, file, indent=4, ensure_ascii=False)
但问题是我不明白如何将它推入机器人的主循环。
【问题讨论】:
标签:
python
asynchronous
discord
discord.py
bots
【解决方案1】:
您可以创建一个定期重复您的功能的任务
import asyncio
def save(array):
while(True): # repeat endlessly
with open('users.json', 'w') as file:
json.dump(array, file, indent=4, ensure_ascii=False)
await asyncio.sleep(1000) # wait 1000 seconds
@client.event
async def on_ready():
# do your on_ready routine
array = [] # whatever you need to write in there
client.loop.create_task(save(array))