Python 如何让discord机器人在一个月的某一天执行代码

Python 如何让discord机器人在一个月的某一天执行代码,python,datetime,discord,discord.py,Python,Datetime,Discord,Discord.py,我希望我的机器人在每个月的某一天发送一条消息,在这种情况下是1号 在下面的代码中,我只使用一个while循环来查看它是否是一天,如果不是的话,只需重复直到它是一天,然而,在这种情况下,它将被卡在循环中,机器人将不会启动。我也很确定,如果我这样做,它只会工作一次。我如何让一些代码在一个月中的某一天运行,而不被困在循环中,并且每个月都在一个不协调的机器人中工作 import discord from discord.ext import commands import timevaribels fr

我希望我的机器人在每个月的某一天发送一条消息,在这种情况下是1号

在下面的代码中,我只使用一个while循环来查看它是否是一天,如果不是的话,只需重复直到它是一天,然而,在这种情况下,它将被卡在循环中,机器人将不会启动。我也很确定,如果我这样做,它只会工作一次。我如何让一些代码在一个月中的某一天运行,而不被困在循环中,并且每个月都在一个不协调的机器人中工作

import discord
from discord.ext import commands
import timevaribels
from datetime import datetime

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

channel_id = 852604482637398056

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

async def annoucment():
    await client.wait_until_ready()
    channel = client.get_channel(channel_id)
    while datetime.now() != timevaribels.custom_date_time:
      pass
    else:
        await channel.send('message')

async def backround():
    await annoucment()

client.loop.create_task(backround())
client.run('key')

您可以使用discord.ext的任务

例如:

import discord
from discord.ext import commands, tasks
from datetime import datetime

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

channel_id = 852604482637398056
targetDate = '06-11' # month / day

def GetDate():
    return datetime.today().strftime('%m-%d')

@client.event
async def on_ready():
    # I recommend put this on construtor
    DateChecker.start() # Start the loop 
    print('Ready')

@tasks.loop(hours=24)
async def DateChecker():
    print("Running loop...")

    if GetDate() == targetDate:
        channel = client.get_channel(channel_id) 
        await channel.send("Hello World!")

client.run('key')

@Goion ti看起来你不能在一个月的某一天运行此功能,最大值是一周中的每一天,这可能会起作用,但它肯定可以。浏览文档。计划库正在阻塞,您不应该在异步代码中使用它。对于目标日期变量“%m-1”是否有效?或者只能是一年中的某一天当然,你可以设置日期、月份或年份。您只需要更改GetDate datetime对象(%y,%m或%d)和targetDate。