Fix snitch test to not send to wrong user, implement Discord DM sending for snitches
This commit is contained in:
58
bot/bot.py
58
bot/bot.py
@@ -652,12 +652,70 @@ async def beforePresenceTrackingLoop():
|
||||
await client.wait_until_ready()
|
||||
|
||||
|
||||
@tasks.loop(seconds=30)
|
||||
async def snitchCheckLoop():
|
||||
"""Check for pending snitch notifications and send them."""
|
||||
try:
|
||||
import core.snitch as snitch_core
|
||||
import core.postgres as postgres
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# Get pending snitches from the last 5 minutes that haven't been sent
|
||||
cutoff = datetime.utcnow() - timedelta(minutes=5)
|
||||
pending_snitches = postgres.select("snitch_log", where={"delivered": False})
|
||||
|
||||
for snitch in pending_snitches:
|
||||
sent_at = snitch.get("sent_at")
|
||||
if not sent_at or sent_at < cutoff:
|
||||
continue
|
||||
|
||||
contact_id = snitch.get("contact_id")
|
||||
if not contact_id:
|
||||
continue
|
||||
|
||||
# Get contact details
|
||||
contacts = postgres.select("snitch_contacts", {"id": contact_id})
|
||||
if not contacts:
|
||||
continue
|
||||
|
||||
contact = contacts[0]
|
||||
if contact.get("contact_type") != "discord":
|
||||
continue
|
||||
|
||||
discord_user_id = contact.get("contact_value")
|
||||
message = snitch.get("message_content", "Snitch notification")
|
||||
|
||||
try:
|
||||
# Send Discord DM
|
||||
user = await client.fetch_user(int(discord_user_id))
|
||||
if user:
|
||||
await user.send(message)
|
||||
# Mark as delivered
|
||||
postgres.update(
|
||||
"snitch_log", {"delivered": True}, {"id": snitch.get("id")}
|
||||
)
|
||||
print(
|
||||
f"Snitch sent to {contact.get('contact_name')} (Discord: {discord_user_id})"
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error sending snitch to {discord_user_id}: {e}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error in snitch check loop: {e}")
|
||||
|
||||
|
||||
@snitchCheckLoop.before_loop
|
||||
async def beforeSnitchCheckLoop():
|
||||
await client.wait_until_ready()
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
print(f"Bot logged in as {client.user}")
|
||||
loadCache()
|
||||
backgroundLoop.start()
|
||||
presenceTrackingLoop.start()
|
||||
snitchCheckLoop.start()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user