小编Log*_*man的帖子

安排异步任务每 X 秒执行一次?

我正在尝试创建一个 python discord bot,它将每 X 秒检查一次活跃成员,并根据他们的在线时间奖励成员。我正在使用 asyncio 来处理聊天命令,这一切正常。我的问题是找到一种方法,以异步方式每 X 秒安排一次对活动成员的检查

我已经阅读了 asnycio 文档,但这是我第一次使用它,而且我很难把头放在任务和循环以及协同例程等上。

@client.event
async def on_message(message):

    # !gamble command
    if message.content.startswith('!gamble'):

        ...code that works....

    # !help command
    elif message.content == '!help':

         ...code that works....

    # !balance command
    elif message.content == '!balance':

      ...code that works....

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

# Do this every X seconds to give online users +1 points
async def periodic_task():
      TODO
Run Code Online (Sandbox Code Playgroud)

我的目标是让机器人能够处理通过聊天给它的命令,同时每隔 X 秒触发一个与 Discord 服务器中的聊天命令或事件无关的功能。我知道如何让函数内部的代码实现我的目标,只是不知道如何触发它

python python-asyncio discord

6
推荐指数
2
解决办法
6436
查看次数

标签 统计

discord ×1

python ×1

python-asyncio ×1