Issue
I want my discord python bot to send a specific message in a channel 2 times a day. First at 12 o'clock and then 18 o'clock in Europe/Berlin time (or just from the server time).
How do I make it? I tried many things but I can't find a way to do it.
Solution
You can use APScheduler
and Cron
to schedule your messages to be sent at a specific time, like 12:00 AM
Docs: APScheduler
, Cron
Here is an example:
#async scheduler so it does not block other events
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix="!")
async def func():
await bot.wait_until_ready()
c = bot.get_channel(channel_id)
await c.send("Your Message")
@bot.event
async def on_ready():
print("Ready")
#initializing scheduler
scheduler = AsyncIOScheduler()
#sends "Your Message" at 12PM and 18PM (Local Time)
scheduler.add_job(func, CronTrigger(hour="12, 18", minute="0", second="0"))
#starting the scheduler
scheduler.start()
Answered By - Just for fun
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.