discord.py 如何让机器人在特定日期自动发送消息?

时间:2021-03-10 00:38:56

标签: python discord discord.py

我在处理日期的机器人上工作。我希望我的不和谐机器人在没有命令的情况下自动发送一些消息。例如,有如何使用命令对日期做出反应:

bot = commands.Bot(command_prefix='!')

 @bot.command()
 async def badd(ctx): 
  today = date.today()
  d1 = today.strftime("%d/%m")
  if d1 == '10/03':
   await ctx.send('Its working')
  else:
   await ctx.send('False')

所以当用户输入“!badd”时 - 机器人发送消息。有没有什么办法不用向bot发送命令呢?

1 个答案:

答案 0 :(得分:1)

截至目前,您可以使用 task 扩展为每个时间段创建一个循环,但您必须计算它:

from discord.ext import commands, tasks

bot = commands.Bot("!")

channelId = #put the id of the channel in here

@tasks.loop(hours=168) #in this example for every week  
async def day_schedule():
    message_channel = bot.get_channel(channelId)
    await message_channel.send("Your message")