AI-Discord-Bot/scheduler.py

71 lines
2.3 KiB
Python

# scheduler.py
import asyncio
import random
import yaml
import datetime
from ai import get_ai_response
def load_settings():
with open("settings.yml", "r", encoding="utf-8") as f:
return yaml.safe_load(f)["scheduler"]
last_post_time = None
post_chance = None
async def start_scheduler(bot):
settings = load_settings()
if not settings["enabled"]:
print("🛑 Scheduler disabled in config.")
return
interval = settings["interval_minutes"]
channel = bot.get_channel(settings["channel_id"])
mode = settings.get("mode", "simple")
use_ai = settings.get("use_ai", True)
global last_post_time, post_chance
last_post_time = datetime.datetime.utcnow()
post_chance = settings.get("probabilistic", {}).get("start_chance", 0.05)
await bot.wait_until_ready()
print(f"🕒 Delta Scheduler started in {mode.upper()} mode.")
while not bot.is_closed():
now = datetime.datetime.utcnow()
should_post = False
if mode == "simple":
should_post = True
elif mode == "probabilistic":
if random.random() < post_chance:
should_post = True
else:
post_chance += settings["probabilistic"]["increase_per_interval"]
elif mode == "inactivity":
if channel.last_message:
last_msg_time = channel.last_message.created_at.replace(tzinfo=None)
idle_time = (now - last_msg_time).total_seconds() / 60
if idle_time >= settings["inactivity"]["threshold_minutes"]:
should_post = True
if should_post:
last_post_time = now
post_chance -= settings.get("probabilistic", {}).get("decay_on_post", 0.02)
post_chance = max(post_chance, 0.01)
# Generate or choose message
if use_ai:
prompt = "Post a short chaotic or motivational message in the voice of Delta, the RGB catgirl."
message = get_ai_response(prompt)
else:
message = random.choice(settings.get("messages", ["Hello from Delta."]))
await channel.send(message)
print(f"📤 Scheduled message sent to #{channel.name}: {message}")
await asyncio.sleep(interval * 60)