Python discord.py如何每天在特定时间发送消息

Python discord.py如何每天在特定时间发送消息,python,discord.py,Python,Discord.py,我想创建一个discord机器人,每天在特定时间发送2条消息。下面的代码将使消息进入循环,例如每5秒发送一条消息。如何设置每天发送消息的特定时间,例如,消息1在下午6点发送,消息2在上午10点发送。 我找到了这个代码,但没有找到我想要的 import discord from discord.ext import commands, tasks bot = commands.Bot("$") channel_id = #Any ID #Message 1 @tasks.l

我想创建一个discord机器人,每天在特定时间发送2条消息。下面的代码将使消息进入循环,例如每5秒发送一条消息。如何设置每天发送消息的特定时间,例如,消息1在下午6点发送,消息2在上午10点发送。 我找到了这个代码,但没有找到我想要的

import discord
from discord.ext import commands, tasks

bot = commands.Bot("$")
channel_id = #Any ID

#Message 1
@tasks.loop(seconds=5)
async def called_once_a_day():
  message_channel = bot.get_channel(channel id)
  await message_channel.send("test 1")

@called_once_a_day.before_loop
async def before():
    await bot.wait_until_ready()
    print("Finished waiting")
#Message 2 
@tasks.loop(seconds=10)
async def called_once_a_day2():
    message_channel = bot.get_channel(channel id)
    await message_channel.send("test 2")

@called_once_a_day2.before_loop
async def before():
    await bot.wait_until_ready()
    print("Finished waiting")




called_once_a_day.start()
called_once_a_day2.start()

bot.run('token')

你必须在循环之前做一个
,这样它第一次运行时就会准时,然后每24小时做一次循环。这里有一个例子

导入异步IO
将日期时间导入为dt
@机器人事件
_ready()上的异步定义:
打印(“登录为”)
打印(bot.user.name)
打印(“----”)
msg1.start()
#信息1
@tasks.loop(小时=24)
异步def msg1():
message_channel=bot.get_channel(705524214270132367)
等待消息\u通道发送(“测试1”)
@msg1.0前循环
_msg1()之前的异步定义:
对于范围内(60*60*24):#循环一天
如果dt.datetime.now().hour==10+12:#24小时格式
打印(‘时间到了’)
返回
等待asyncio.sleep(1)#在再次循环之前等待一秒钟。你可以做得更多

这里有一个简单的实现——每天,它都会休眠到目标时间,然后发送消息

来自discord.ext导入命令
从datetime导入datetime、time、timedelta
导入异步
bot=commands.bot(command_prefix=“$”)
时间=时间(18,0,0)#下午6:00
频道id=1#把你的频道id放在这里
异步定义每天调用一次()
等待机器人。等待直到准备就绪()#确保你的公会缓存已准备就绪,以便可以通过获取通道找到通道
channel=bot.get_channel(channel_id)#注意:bot.get_guild(guild_id)和get_channel(channel_id)更有效,因为涉及的循环更少,但get_channel仍然可以正常工作
等待频道。发送(“您的信息在此”)
异步定义后台任务():
now=datetime.utcnow()
if now.time()>WHEN:#确保循环不会在{WHEN}之后启动,因为它将立即发送第一次,因为负秒将使睡眠立即产生
明天=datetime.combine(now.date()+timedelta(days=1),时间(0))
秒=(明天-现在)。总秒数()#到明天(午夜)的秒数
等待asyncio.sleep(秒)#睡眠到明天,然后循环将开始
尽管如此:
now=datetime.utcnow()#如果这很重要,您可以执行now()或特定的时区,但我将把它留给utcnow
target_time=datetime.combine(now.date(),WHEN)#今天下午6:00(UTC)
到目标时间的秒数=(目标时间-现在)。总秒数()
等待asyncio.sleep(在达到目标之前的秒数)#在达到目标时间之前睡眠
wait called_one_a_day()#调用发送消息的helper函数
明天=datetime.combine(now.date()+timedelta(days=1),时间(0))
秒=(明天-现在)。总秒数()#到明天(午夜)的秒数
等待asyncio.sleep(秒)#直到明天再休眠,然后循环将开始新的迭代
如果名称=“\uuuuu main\uuuuuuuu”:
bot.loop.create_任务(后台_任务())
bot.run('token')

我创建了一个[Discord bot],它在特定时间发送消息,在该时间到来之前,它将处于睡眠状态。我创建的代码很小,很容易理解
代码:

import discord
from datetime import datetime

client = discord.Client()
token = "" #enter your bot's token and it should be a string
channel_id = #enter your channel id and it should be a integer   

def time_module():
    print("time module in use")
    while True:created
        current_time = datetime.now().strftime("%H:%M")#hour %H min %M sec %S am:pm %p 
        if current_time == "00:00": # enter the time you wish 
            print("time module ended")
            break

time_module()

@client.event
async def on_ready():

    print("bot:user ready == {0.user}".format(client))
    channel = client.get_channel(channel_id)
    await channel.send("message")
    

client.run(token)

您需要计算现在的时间,以及计划的时间,然后每隔10分钟或更少的时间检查计划的时间是否符合现在的时间

检查此链接以查看python的日期时间库

并检查此链接以查看如何使用@tasks.loop(后台任务)的示例

使用dateTime库获取当前时间

now=datetime.datetime.now()#将存储调用它的时间
#如果您想输入具体时间:
提醒肉=日期时间。日期时间(2021,5,4,17,24)#年、月、日、小时、分钟、秒

如果我犯了一些小错误,请告诉我,我不习惯这种布局,可能会错过一些东西