Gacha_response changed to response
This commit is contained in:
parent
6b6777cb89
commit
24309ce900
3 changed files with 47 additions and 40 deletions
|
@ -4,6 +4,7 @@ import misskey
|
||||||
from parsing import parse_notification
|
from parsing import parse_notification
|
||||||
from db_utils import get_or_create_user, add_pull, get_config, set_config
|
from db_utils import get_or_create_user, add_pull, get_config, set_config
|
||||||
from client import client_connection
|
from client import client_connection
|
||||||
|
from response import generate_response
|
||||||
|
|
||||||
# Initialize the Misskey client
|
# Initialize the Misskey client
|
||||||
client = client_connection()
|
client = client_connection()
|
||||||
|
@ -44,15 +45,49 @@ def stream_notifications():
|
||||||
instance = host if host else "local"
|
instance = host if host else "local"
|
||||||
|
|
||||||
if instance in whitelisted_instances or instance == "local":
|
if instance in whitelisted_instances or instance == "local":
|
||||||
note = notification.get("note", {}).get("text", "")
|
note = notification.get("note", {})
|
||||||
|
note_text = note.get("text", "")
|
||||||
|
note_id = note.get("id")
|
||||||
notif_type = notification.get("type", "unknown")
|
notif_type = notification.get("type", "unknown")
|
||||||
|
|
||||||
|
# We want the visibility to be related to the type that was received (so if
|
||||||
|
# people don't want to dump a bunch of notes on home they don't have to)
|
||||||
|
|
||||||
|
visibility = notification["note"]["visibility"]
|
||||||
|
if visibility != "specified":
|
||||||
|
visibility = "home"
|
||||||
|
|
||||||
print(f"📨 [{notif_type}] from @{username}@{instance}")
|
print(f"📨 [{notif_type}] from @{username}@{instance}")
|
||||||
print(f"💬 {note}")
|
print(f"💬 {note_text}")
|
||||||
print("-" * 30)
|
print("-" * 30)
|
||||||
|
|
||||||
# 🧠 Send to the parser
|
|
||||||
parse_notification(notification,client)
|
# We get the type of notification to filter the ones that we actually want
|
||||||
|
# to parse
|
||||||
|
|
||||||
|
notif_type = notification.get("type")
|
||||||
|
if notif_type in ('mention', 'reply'):
|
||||||
|
# 🧠 Send to the parser
|
||||||
|
parsed_command = parse_notification(notification,client)
|
||||||
|
# Get the response
|
||||||
|
response = generate_response(parsed_command)
|
||||||
|
if isinstance(response, str):
|
||||||
|
client.notes_create(
|
||||||
|
text=response,
|
||||||
|
reply_id=note_id,
|
||||||
|
visibility=visibility
|
||||||
|
)
|
||||||
|
elif response:
|
||||||
|
client.notes_create(
|
||||||
|
text=response[0],
|
||||||
|
reply_id=note_id,
|
||||||
|
visibility=visibility,
|
||||||
|
file_ids=response[1]
|
||||||
|
#visible_user_ids=[] #todo: write actual visible users ids so pleromers can use the bot privately
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print(f"⚠️ Blocked notification from untrusted instance: {host}")
|
print(f"⚠️ Blocked notification from untrusted instance: {host}")
|
||||||
|
@ -70,7 +105,7 @@ def stream_notifications():
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"An exception has occured: {e}\n{traceback.format_exc()}")
|
print(f"An exception has occured: {e}\n{traceback.format_exc()}")
|
||||||
time.sleep(5)
|
time.sleep(2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,11 @@
|
||||||
import random, re
|
import random, re
|
||||||
import config
|
import config
|
||||||
from gacha_response import gacha_response
|
|
||||||
|
|
||||||
def parse_notification(notification,client):
|
def parse_notification(notification,client):
|
||||||
'''Parses any notifications received by the bot and sends any commands to
|
'''Parses any notifications received by the bot and sends any commands to
|
||||||
gacha_response()'''
|
gacha_response()'''
|
||||||
|
|
||||||
# We get the type of notification to filter the ones that we actually want
|
|
||||||
# to parse
|
|
||||||
|
|
||||||
notif_type = notification.get("type")
|
|
||||||
if not notif_type in ('mention', 'reply'):
|
|
||||||
return # Ignore anything that isn't a mention
|
|
||||||
|
|
||||||
# We want the visibility to be related to the type that was received (so if
|
|
||||||
# people don't want to dump a bunch of notes on home they don't have to)
|
|
||||||
visibility = notification["note"]["visibility"]
|
|
||||||
if visibility != "specified":
|
|
||||||
visibility = "home"
|
|
||||||
|
|
||||||
# Get the full Activitypub ID of the user
|
# Get the full Activitypub ID of the user
|
||||||
user = notification.get("user", {})
|
user = notification.get("user", {})
|
||||||
|
@ -50,22 +38,4 @@ def parse_notification(notification,client):
|
||||||
command = parts[0].lower() if parts else None
|
command = parts[0].lower() if parts else None
|
||||||
arguments = parts[1:] if len(parts) > 1 else []
|
arguments = parts[1:] if len(parts) > 1 else []
|
||||||
|
|
||||||
# TODO: move response generation to a different function
|
return [command,full_user, arguments, note_obj]
|
||||||
response = gacha_response(command.lower(),full_user, arguments, note_obj)
|
|
||||||
if not response:
|
|
||||||
return
|
|
||||||
|
|
||||||
if isinstance(response, str):
|
|
||||||
client.notes_create(
|
|
||||||
text=response,
|
|
||||||
reply_id=note_id,
|
|
||||||
visibility=visibility
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
client.notes_create(
|
|
||||||
text=response[0],
|
|
||||||
reply_id=note_id,
|
|
||||||
visibility=visibility,
|
|
||||||
file_ids=response[1]
|
|
||||||
#visible_user_ids=[] #todo: write actual visible users ids so pleromers can use the bot privately
|
|
||||||
)
|
|
||||||
|
|
|
@ -26,11 +26,13 @@ def is_float(val):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
# TODO: See issue #3, separate command parsing from game logic.
|
def generate_response(parsed_command):
|
||||||
def gacha_response(command,full_user, arguments,note_obj):
|
|
||||||
'''Parses a given command with arguments, processes the game state and
|
'''Given a command with arguments, processes the game state and
|
||||||
returns a response'''
|
returns a response'''
|
||||||
|
|
||||||
|
command, full_user, arguments, note_obj = parsed_command
|
||||||
|
|
||||||
if command == "roll":
|
if command == "roll":
|
||||||
user_id = get_or_create_user(full_user)
|
user_id = get_or_create_user(full_user)
|
||||||
character_id, character_name, file_id, rarity = get_character()
|
character_id, character_name, file_id, rarity = get_character()
|
Loading…
Add table
Reference in a new issue