discord.py:函数等待到特定的日期和时间



我又有一个问题:(我计划了一个机器人,当用户告诉机器人什么时候,它会向频道输出消息。我会给你看代码,然后我会告诉你我的问题:

import discord
import datetime
import time
import asyncio
import random
from discord.ext import commands
from discord import Embed
TOKEN = 'mytoken'
intents = discord.Intents().all()
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(client.user.name)
print(client.user.id)
@client.event
async def on_message(message):
def check(m):
return m.channel == message.channel and m.author != client.user
if message.author == client.user:
return
if message.content.startswith("!start"):
await message.channel.send('Please type in the Enddate (TT.MM.JJ):')
enddateinput = await client.wait_for('message', check=check, timeout=30)
enddate = enddateinput.content
await message.channel.send('Please type in the Endtime (HH:MM):')
endtimeinput = await client.wait_for('message', check=check, timeout=30)
endtime = endtimeinput.content
# *********************************************
# I dont know how to check the time with the subfunction time_check :(
# The optimal result is the function wait at this point until datetime.now(now) = enddate & endtime.
# and then i want the function to continue working.       
# *********************************************
await message.channel.send('Enddate & Endtime reached.')
await message.channel.send('Go on with the rest of the code :).')

async def time_check():
await client.wait_until_ready()
while not client.is_closed():
nowdate = datetime.strftime(datetime.now(), '%d.%m.%y')
nowtime = datetime.strftime(datetime.now(), '%H:%M')       
if (nowdate == enddate) and (nowtime == endtime):
await asyncio.sleep(1)
# *********************************************
#           BACK TO FUNKTION
# *********************************************
else:
await asyncio.sleep(60)

client.run(TOKEN)

问题是,我只需要time_check((来每分钟检查一次endtime/enddate的当前时间。如果达到了结束时间和结束日期,time_check((可以停止工作并返回on_message((继续处理funktion。

我完全不知道如何集成这个check_time((函数。我希望你们中有人能帮助我。

谢谢大家。

这些是您需要更改的函数,而不是每分钟检查一次是否达到结束时间,我只是计算了从当前时间到结束时间等待的总秒数。我还添加了一个检查,看看end time is in the past是否非常重要。总的来说,我相信这是实现你想要的结果的最佳方式。

@client.event
async def on_message(message):
def check(m):
return m.channel == message.channel and m.author != client.user
if message.author == client.user:
return
if message.content.startswith("!start"):
# Get end date.
await message.channel.send("Please type in the Enddate (DD.MM.YYYY):")
enddateinput = await client.wait_for("message", check=check, timeout=30)
enddate = enddateinput.content
# Get end time.
await message.channel.send("Please type in the Endtime (HH:MM):")
endtimeinput = await client.wait_for("message", check=check, timeout=30)
endtime = endtimeinput.content
# Get the two datetime objects, current and endtime.
endtime_obj = datetime.datetime.strptime(
".".join([enddate, endtime]), "%d.%m.%Y.%H:%M"
)
currtime_obj = datetime.datetime.now()
if not (endtime_obj > currtime_obj):  # Check if end time is in the past.
await message.channel.send("End time can not be in the past!")
# Send the seconds to wait into the time check function
time_to_wait = endtime_obj - currtime_obj  # The time delta (difference).
await time_check(message, time_to_wait.total_seconds())

async def time_check(message_obj, secs_to_wait: float):
await client.wait_until_ready()
await asyncio.sleep(secs_to_wait)
await message_obj.channel.send("Enddate & Endtime reached.")
await message_obj.channel.send("Go on with the rest of the code :).")

最新更新