misskey-microblogger/src/User/User.py
2024-11-09 13:20:08 +01:00

66 lines
No EOL
2.4 KiB
Python

import json
import random
import typing
from pathlib import Path
from datetime import datetime
from misskey.enum import NoteVisibility
from ..Enums import RelationshipType, Intent
USER_CONFIG_DIR = Path.cwd() / "data" / "users"
DEFAULT_REACTION = ""
class User():
def __init__(self, username: str):
self.username = username
# Load user config from file
with open(USER_CONFIG_DIR / f"{self.username}.json", "r") as f:
self.config = json.load(f)
# Check if the user is currently online given their time intervals
def is_online(self) -> bool:
# Find the first time interval that is within the current time
for interval in self.config["online"]["intervals"]:
time_now = datetime.now()
# Convert current hours and minutes to minutes
time_now = time_now.hour * 100 + time_now.minute
# Generate a random float between from and two floats and convert time to minutes
time_start = random.uniform(interval["start"]["from"], interval["start"]["to"]) * 100
time_end = random.uniform(interval["end"]["from"], interval["end"]["to"]) * 100
# Bail out if current time is in range of interval
if (time_now > time_start and time_now < time_end):
return True
# Current time was not in range of any configured intervals
return False
def get_post_cooldown(self, visibility: NoteVisibility = NoteVisibility.PUBLIC) -> int:
return self.config["actions"][Intent.POST.value][visibility.value]["cooldown"]
def get_partner(self) -> str | None:
return self.config["relationships"]["partner"]
def get_friends(self) -> list:
return self.config["relationships"]["friends"]
def get_enemies(self) -> list:
return self.config["relationships"]["enemies"]
# Get the relationship with another user by username
def get_relationship_with_user(self, username: str) -> RelationshipType:
if (username == self.get_partner()): return RelationshipType.PARTNER
elif (username in self.get_friends()): return RelationshipType.FRIEND
elif (username in self.get_enemies()): return RelationshipType.ENEMY
return RelationshipType.NEUTRAL
# Get prefered reaction for user against another user
def get_reaction(self, note: dict) -> str:
relationship = self.get_relationship_with_user(note["user"]["username"])
reaction = self.config["actions"][Intent.REACT.value]["prefrerred_reaction"][relationship.value]
# Return preferred reaction if set else return default
return reaction if reaction and len(reaction) == 1 else DEFAULT_REACTION