简体   繁体   中英

discord.py How to make bot automatically send message on a specific day?

I am working at my bot that is working with dates. I want my discord bot to send some message automatically, without command. For example, there is how to react on date with command:

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')

So when user enter ".badd " - bot send message? Is there any way to do it without sending a command to bot?

As of right now, You could use the task extension to create a loop for every period of time, but you would have to calculate it:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM