BaraHinozuka
BaraHinozuka

Reputation: 63

Using discord.py to send a message everyday at a specified time

I'm trying to make a Discord bot that will send a message at a channel at a given time, I found previous codes but they don't seem to work for me since they give indention errors.

I'm not an experienced programmer and any answer would be very helpful.

Upvotes: 0

Views: 321

Answers (1)

miguel-mrtnez
miguel-mrtnez

Reputation: 11

I would recommend you to look for AsyncIOScheduler, from the apscheduler module. If you are using a class based bot, the code below should work:

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from discord.ext.commands import Bot as BotBase


class Bot(BotBase):
    def __init__(self):
        super().__init__(command_prefix=your_command)
        self.scheduler = AsyncIOScheduler()

    async def on_ready(self):
        self.scheduler.add_job(self.birthday, CronTrigger(hour=0))
        self.scheduler.start()

    async def action(self):
        channel = self.get_channel(channel_id)
        await channel.send('Your message')

The scheduler will execute the action method every time the hours counter of the CronTrigger reaches 0.

Upvotes: 1

Related Questions