不协调时间表 [英] Discord.py Time Schedule

查看:35
本文介绍了不协调时间表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有比我在代码中使用的闹钟更好的用python创建闹钟的方法?它工作正常,但是我想知道是否还有更好的东西,我也想将这段代码分离出来,并放入一个齿轮中....

Is there a better way to create a alarm clock in python than the one I'm using in my code? It is working fine but I wonder if there is something better , also I want to separate this code and make it in to a cog....

import asyncio
from datetime import datetime
from discord.ext import commands

TOKEN = 'XXX'

client = commands.Bot(command_prefix='.')

alarm_time = '23:33'#24hrs
channel_id = '51599XXXXX5036697'

@client.event
async def on_ready():
    print('Bot Online.')


async def time_check():
    await client.wait_until_ready()
    while not client.is_closed:
        now = datetime.strftime(datetime.now(), '%H:%M')
        channel = client.get_channel(channel_id)
        messages = ('Test')
        if now == alarm_time:
           await client.send_message(channel, messages)
             time = 90
        else:
           time = 1
        await asyncio.sleep(time)


client.loop.create_task(time_check())

client.run(TOKEN)

推荐答案

您可以使用

What you could do is use the sched, for example:

import time
import sched
s = sched.scheduler(time.perf_counter, time.sleep)
s.enter(60, 1, action_function, (args))
s.run()

上面的代码以 s 的形式启动调度程序,该调度程序使用 time.perf_couter 来获取当前时间,并使用 time.sleep 来执行此操作.延迟.

The code above starts a scheduler as s, which uses time.perf_couter to get the current time, and time.sleep for doing the delay.

使用调度程序时,您需要传递至少3个参数,第一个参数是以秒为单位的标准时间,第二个参数是优先级(优先级最高的预定事件将首先执行,第三个参数是函数在延迟后执行.

When you use the scheduler you need to pass it at least 3 arguments, the first being the daly time in seconds, the second being it's priority (scheduled events with the highest prioroty are executed first, and the third argument is the function to be executed after the delay.

还有两个可选参数,它们是参数的元组或关键字参数的字典,这两个参数都将传递给在延迟后执行的函数.

There can be two more optional arguments being a tuple of arguments, or a dict of keyword arguments, both of these arguments will get passed to the function that will be executed after the delay.

我已经使用同一库来实现IRC bot中的定时禁令之类的事情,因此它也应适用于您的Discord bot.

I have used this same library for implementing things such as timed bans within IRC bots, so it should also be applicable to your Discord bot.

这是一个应该使用您的代码的示例(我不使用不和谐,因此不能真正测试整个代码,只是片段).

Here is an example that should work (I don't use discord and such so don't can't really test the whole code, just snippets) using your code:

import asyncio
from datetime import datetime, timedelta
from discord.ext import commands
import time
import sched

TOKEN = 'XXX'

client = commands.Bot(command_prefix='.')

alarm_time = '23:33'#24hrs
channel_id = '51599XXXXX5036697'

@client.event
async def on_ready():
    print('Bot Online.')


async def time_check():
    await client.wait_until_ready()
    while not client.is_closed:
        channel = client.get_channel(channel_id)
        messages = ('Test')
        f = '%H:%M'

        now = datetime.strftime(datetime.now(), f)
        # get the difference between the alarm time and now
        diff = (datetime.strptime(alarm_time, f) - datetime.strptime(now, f)).total_seconds()

        # create a scheduler
        s = sched.scheduler(time.perf_counter, time.sleep)
        # arguments being passed to the function being called in s.enter
        args = (client.send_message(channel, message), )
        # enter the command and arguments into the scheduler
        s.enter(seconds, 1, client.loop.create_task, args)
        s.run() # run the scheduler, will block the event loop


client.loop.create_task(time_check())

client.run(TOKEN)

这篇关于不协调时间表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆