Last active
August 30, 2024 11:02
-
-
Save arnodeceuninck/c3b06e30d66045fba56789f43d04260e to your computer and use it in GitHub Desktop.
Discord bot in python, send a specific message at given time/day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from apscheduler.schedulers.asyncio import AsyncIOScheduler | |
from discord.ext import commands | |
discord_bot_token = "dQw4w9WgXcQdQw4w9WgXcQdQ.dQw4w9.dQw4w9WgX-dQw4w9WgXcQdQw4w9" | |
bot = commands.Bot(command_prefix="-") | |
@bot.event | |
async def on_ready(): | |
print(f"Ready!") | |
# TODO: Recover planned messages after crashes | |
async def send_message(channel, message): | |
await channel.send(message) | |
def schedule_message(send_time, channel, message): | |
scheduler = AsyncIOScheduler() | |
scheduler.add_job(send_message, 'date', run_date=send_time, args=[channel, message]) | |
scheduler.start() | |
@bot.command() | |
async def schedule(ctx, time_str, message, date_str=None, channel_id=None): | |
""" | |
Schedule a message in current chat at a specific time | |
@:param time_str: The time to send the message, HH:MM format, must still be today | |
@:param message: Must be in quotation marks, the message you want to send | |
Usage: | |
-schedule <time> "<message>" | |
Example: | |
-schedule 16:00 "Hello there :)" | |
""" | |
# Get the date | |
if date_str is None: | |
date = datetime.now() | |
else: | |
date = datetime.strptime(date_str, "%d/%m/%Y") | |
run_time = datetime.strptime(time_str, '%H:%M').replace(year=date.year, month=date.month, day=date.day) | |
# Check if it's in the future | |
now = datetime.now() | |
delay = (run_time - now).total_seconds() | |
assert delay > 0 | |
# Get the channel | |
if channel_id is not None: | |
channel = bot.get_channel(int(channel_id)) | |
else: | |
channel = ctx.channel | |
assert channel is not None | |
# Schedule the message | |
schedule_message(run_time, channel, message) | |
# Confirm | |
print(f"Scheduled a message at {run_time} in channel {channel}: {message}") | |
await ctx.channel.send(f"All set, message wil be sent in {delay / 60:.2f} minutes (unless this bot crashes in meantime)!") | |
bot.run(discord_bot_token) |
Lmao nice and definitely real token, totally not the id of a certain youtube video
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@EricFalkenberg Thanks for checking this! The token is made up, so safe to share
Fun fact: The token was based on the id of this video: https://www.youtube.com/watch?v=dQw4w9WgXcQ