Add adaptive medication timing, Discord presence tracking, and nagging system
This commit is contained in:
83
bot/bot.py
83
bot/bot.py
@@ -577,5 +577,88 @@ async def beforeBackgroundLoop():
|
||||
await client.wait_until_ready()
|
||||
|
||||
|
||||
# ==================== Discord Presence Tracking ====================
|
||||
|
||||
|
||||
async def update_presence_tracking():
|
||||
"""Track Discord presence for users with presence tracking enabled."""
|
||||
try:
|
||||
import core.adaptive_meds as adaptive_meds
|
||||
import core.postgres as postgres
|
||||
|
||||
# Get all users with presence tracking enabled
|
||||
settings = postgres.select(
|
||||
"adaptive_med_settings", {"presence_tracking_enabled": True}
|
||||
)
|
||||
|
||||
for setting in settings:
|
||||
user_uuid = setting.get("user_uuid")
|
||||
|
||||
# Get user's Discord ID from notifications table
|
||||
notif_settings = postgres.select("notifications", {"user_uuid": user_uuid})
|
||||
if not notif_settings:
|
||||
continue
|
||||
|
||||
discord_user_id = notif_settings[0].get("discord_user_id")
|
||||
if not discord_user_id:
|
||||
continue
|
||||
|
||||
# Get the user from Discord
|
||||
try:
|
||||
discord_user = await client.fetch_user(int(discord_user_id))
|
||||
if not discord_user:
|
||||
continue
|
||||
|
||||
# Check if user is online
|
||||
is_online = discord_user.status != discord.Status.offline
|
||||
|
||||
# Get current presence from DB
|
||||
presence = adaptive_meds.get_user_presence(user_uuid)
|
||||
was_online = presence.get("is_currently_online") if presence else False
|
||||
|
||||
# Update presence if changed
|
||||
if is_online != was_online:
|
||||
adaptive_meds.update_user_presence(
|
||||
user_uuid, discord_user_id, is_online
|
||||
)
|
||||
|
||||
# Record the event
|
||||
from datetime import datetime
|
||||
|
||||
event_type = "online" if is_online else "offline"
|
||||
adaptive_meds.record_presence_event(
|
||||
user_uuid, event_type, datetime.utcnow()
|
||||
)
|
||||
|
||||
print(
|
||||
f"Presence update: User {user_uuid} is now {'online' if is_online else 'offline'}"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error tracking presence for user {user_uuid}: {e}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error in presence tracking loop: {e}")
|
||||
|
||||
|
||||
@tasks.loop(seconds=30)
|
||||
async def presenceTrackingLoop():
|
||||
"""Track Discord presence every 30 seconds."""
|
||||
await update_presence_tracking()
|
||||
|
||||
|
||||
@presenceTrackingLoop.before_loop
|
||||
async def beforePresenceTrackingLoop():
|
||||
await client.wait_until_ready()
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
print(f"Bot logged in as {client.user}")
|
||||
loadCache()
|
||||
backgroundLoop.start()
|
||||
presenceTrackingLoop.start()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
client.run(DISCORD_BOT_TOKEN)
|
||||
|
||||
12
bot/config.json
Normal file
12
bot/config.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"openrouter_api_key": "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",
|
||||
"embedding_file": "dbt_knowledge.json",
|
||||
"models": {
|
||||
"generator": "moonshotai/kimi-k2.5",
|
||||
"jury_clinical": "z-ai/glm-5",
|
||||
"jury_safety": "deepseek/deepseek-v3.2",
|
||||
"jury_empathy": "openai/gpt-4o-2024-08-06",
|
||||
"jury_hallucination": "qwen/qwen3-235b-a22b-2507"
|
||||
},
|
||||
"system_prompt": "You are a DBT assistant. Answer based ONLY on the provided context."
|
||||
}
|
||||
Reference in New Issue
Block a user