Moved logic into simple on the scheduler dir. Cleaned up code and made things more modular
This commit is contained in:
parent
de74e2a335
commit
c751b7613e
6 changed files with 47 additions and 62 deletions
|
|
@ -1,78 +1,29 @@
|
||||||
# __init__.py formaly scheduler.py
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import asyncio
|
|
||||||
import random
|
|
||||||
import yaml
|
import yaml
|
||||||
import datetime
|
import asyncio
|
||||||
from ai import get_ai_response
|
from . import simple, probabilistic, inactivity
|
||||||
|
|
||||||
def load_settings():
|
def load_settings():
|
||||||
base_dir = os.path.dirname(os.path.dirname(__file__)) # go up one level from /scheduler/
|
base_dir = os.path.dirname(os.path.dirname(__file__)) # go up from /scheduler/
|
||||||
settings_path = os.path.join(base_dir, "settings.yml")
|
settings_path = os.path.join(base_dir, "settings.yml")
|
||||||
with open(settings_path, "r", encoding="utf-8") as f:
|
with open(settings_path, "r", encoding="utf-8") as f:
|
||||||
return yaml.safe_load(f)
|
return yaml.safe_load(f)
|
||||||
|
|
||||||
last_post_time = None
|
|
||||||
post_chance = None
|
|
||||||
|
|
||||||
async def start_scheduler(bot):
|
async def start_scheduler(bot):
|
||||||
settings = load_settings()
|
settings = load_settings()
|
||||||
scheduler_settings = settings["scheduler"]
|
scheduler_settings = settings.get("scheduler", {})
|
||||||
|
|
||||||
if not scheduler_settings.get("enabled", False):
|
if not scheduler_settings.get("enabled", False):
|
||||||
print("🛑 Scheduler disabled in config.")
|
print("🛑 Scheduler disabled in config.")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
mode = scheduler_settings.get("mode", "simple").lower()
|
||||||
interval = scheduler_settings["interval_minutes"]
|
|
||||||
channel_id = scheduler_settings["channel_id"]
|
|
||||||
channel = bot.get_channel(channel_id)
|
|
||||||
mode = scheduler_settings.get("mode", "simple")
|
|
||||||
use_ai = scheduler_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":
|
if mode == "simple":
|
||||||
should_post = True
|
await simple.run(bot, scheduler_settings, settings)
|
||||||
|
|
||||||
elif mode == "probabilistic":
|
elif mode == "probabilistic":
|
||||||
if random.random() < post_chance:
|
await probabilistic.run(bot, scheduler_settings, settings)
|
||||||
should_post = True
|
|
||||||
else:
|
|
||||||
post_chance += settings["probabilistic"]["increase_per_interval"]
|
|
||||||
|
|
||||||
elif mode == "inactivity":
|
elif mode == "inactivity":
|
||||||
if channel.last_message:
|
await inactivity.run(bot, scheduler_settings, settings)
|
||||||
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:
|
else:
|
||||||
message = random.choice(scheduler_settings.get("messages", ["Hello from Delta."]))
|
print(f"❓ Unknown scheduler mode: {mode}")
|
||||||
|
|
||||||
await channel.send(message)
|
|
||||||
print(f"📤 Scheduled message sent to #{channel.name}: {message}")
|
|
||||||
|
|
||||||
await asyncio.sleep(interval * 60)
|
|
||||||
|
|
|
||||||
Binary file not shown.
BIN
src/scheduler/__pycache__/inactivity.cpython-310.pyc
Normal file
BIN
src/scheduler/__pycache__/inactivity.cpython-310.pyc
Normal file
Binary file not shown.
BIN
src/scheduler/__pycache__/probabilistic.cpython-310.pyc
Normal file
BIN
src/scheduler/__pycache__/probabilistic.cpython-310.pyc
Normal file
Binary file not shown.
BIN
src/scheduler/__pycache__/simple.cpython-310.pyc
Normal file
BIN
src/scheduler/__pycache__/simple.cpython-310.pyc
Normal file
Binary file not shown.
|
|
@ -0,0 +1,34 @@
|
||||||
|
import asyncio
|
||||||
|
import random
|
||||||
|
import datetime
|
||||||
|
from ai import get_ai_response
|
||||||
|
|
||||||
|
last_post_time = None
|
||||||
|
|
||||||
|
async def run(bot, scheduler_settings, full_settings):
|
||||||
|
global last_post_time
|
||||||
|
last_post_time = datetime.datetime.utcnow()
|
||||||
|
|
||||||
|
interval = scheduler_settings["interval_minutes"]
|
||||||
|
channel_id = scheduler_settings["channel_id"]
|
||||||
|
channel = bot.get_channel(channel_id)
|
||||||
|
use_ai = scheduler_settings.get("use_ai", True)
|
||||||
|
|
||||||
|
await bot.wait_until_ready()
|
||||||
|
print("📆 Simple scheduler active.")
|
||||||
|
|
||||||
|
while not bot.is_closed():
|
||||||
|
now = datetime.datetime.utcnow()
|
||||||
|
last_post_time = now
|
||||||
|
|
||||||
|
# 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(scheduler_settings.get("messages", ["Hello from Delta."]))
|
||||||
|
|
||||||
|
await channel.send(message)
|
||||||
|
print(f"📤 [Simple] Sent to #{channel.name}: {message}")
|
||||||
|
|
||||||
|
await asyncio.sleep(interval * 60)
|
||||||
Loading…
Reference in a new issue