forked from vlw/misskey-microblogger
61 lines
No EOL
2.1 KiB
Python
61 lines
No EOL
2.1 KiB
Python
import typing
|
|
import random
|
|
|
|
from misskey.enum import NoteVisibility
|
|
|
|
from .User import User
|
|
from ..Enums import Intent
|
|
|
|
ROLL_MAX = 1000
|
|
ROLL_MULTIPLIER = 0.1
|
|
|
|
class UserIntent(User):
|
|
def __init__(self, username: str):
|
|
super().__init__(username)
|
|
|
|
# Do a ranom roll between 0 and ROLL_MAX and test against target percent with multiplier
|
|
@staticmethod
|
|
def _roll(target: int) -> bool:
|
|
return random.randint(0, ROLL_MAX) < target * (target * ROLL_MULTIPLIER)
|
|
|
|
# Don't interact if offline or with notes from bots or self
|
|
def would_interact_with_note(self, note: dict) -> bool:
|
|
# User is not online right now
|
|
if (not self.is_online()):
|
|
return False
|
|
|
|
return not note["user"]["isBot"] and note["user"]["username"] != self.username
|
|
|
|
def would_post_new_note(self, username: str = None) -> bool:
|
|
# Check if user would make a public post if no username is provided
|
|
if (not username):
|
|
return self._roll(self.config["actions"][Intent.POST.value][NoteVisibility.PUBLIC.value]["percent"])
|
|
|
|
# Check if user would send a new DM to username
|
|
relationship = self.get_relationship_with_user(username)
|
|
return self._roll(self.config["actions"][Intent.POST.value][NoteVisibility.SPECIFIED.value]["percent"][relationship.value])
|
|
|
|
def would_reply_to_note(self, note: dict) -> bool:
|
|
if (not self.would_interact_with_note(note)):
|
|
return False
|
|
|
|
visibility = NoteVisibility(note["visibility"])
|
|
relationship = self.get_relationship_with_user(note["user"]["username"])
|
|
|
|
# Get reply percent from config by note visibility and relationship with poster
|
|
percent = self.config["actions"][Intent.REPLY.value][visibility.value]["percent"][relationship.value]
|
|
return self._roll(percent)
|
|
|
|
def would_react_to_note(self, note: dict) -> bool:
|
|
if (not self.would_interact_with_note(note)):
|
|
return False
|
|
|
|
# Don't react to DMs
|
|
if (NoteVisibility(note["visibility"]) == NoteVisibility.SPECIFIED):
|
|
return False
|
|
|
|
relationship = self.get_relationship_with_user(note["user"]["username"])
|
|
|
|
# Get reply percent from config by note visibility and relationship with poster
|
|
percent = self.config["actions"][Intent.REACT.value]["percent"][relationship.value]
|
|
return self._roll(percent) |