discord.py如何使bot在特定日期自动发送消息?

2024-05-17 09:03:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的机器人上工作,它与日期有关。我想让我的discord机器人自动发送一些消息,无需命令。例如,如何使用命令在日期作出反应:

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发送消息时。有没有办法不用向bot发送命令就可以做到这一点


Tags: 命令send消息todaybot机器人await作出反应
1条回答
网友
1楼 · 发布于 2024-05-17 09:03:48

现在,您可以使用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")

相关问题 更多 >