From a0fb0c8711096b286684c5da84d37544e203c4c8 Mon Sep 17 00:00:00 2001 From: SeoulSKY Date: Tue, 16 Jan 2024 21:21:45 -0600 Subject: [PATCH] Implement script to get tgt of Character AI automatically --- README.md | 2 +- firestore/channel.py | 37 +----------------------------------- firestore/exceptions.py | 12 ------------ firestore/user.py | 40 +-------------------------------------- scripts/cai_tgt.py | 30 +++++++++++++++++++++++++++++ scripts/clear_commands.py | 1 + 6 files changed, 34 insertions(+), 88 deletions(-) delete mode 100644 firestore/exceptions.py create mode 100644 scripts/cai_tgt.py diff --git a/README.md b/README.md index 2db51d3..a952736 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Create `.env` file, copy and paste all contents from `.env.example` file, and fi | FIREBASE_CLIENT_X509_CERT_URL | Same as above | | CAI_TOKEN | Token for your Character AI account. Follow the [guide](https://pycai.gitbook.io/welcome/api/values) to learn how to acquire it. | | CAI_CHAR_ID | ID for the character in the Character AI. Follow the [guide](https://pycai.gitbook.io/welcome/api/values) to learn how to acquire it. | -| CAI_TGT | ID for the target in the Character AI. Its value starts with `internal_id:` Follow the [guide](https://pycai.gitbook.io/welcome/api/values) to learn about it. | +| CAI_TGT | ID for the target in the Character AI. Its value starts with `internal_id:`. Run `scripts/cai_tgt.py` to get it. | ### Running with [Docker](https://www.docker.com) and [Docker Compose](https://docs.docker.com/compose/install/) (Recommended) diff --git a/firestore/channel.py b/firestore/channel.py index 2dcd7eb..e25126b 100644 --- a/firestore/channel.py +++ b/firestore/channel.py @@ -1,9 +1,6 @@ """ Provides functions to interact with the channel configs in the firestore -Classes: - Channel - Functions: get_collection() has_channel() @@ -14,39 +11,7 @@ from google.cloud.firestore_v1 import AsyncCollectionReference from firestore import db - - -class Channel: - """ - A wrapper class to represent channel configs in the database - """ - - def __init__(self, channel_id: int, translate_to: list[str] = None): - self.channel_id = channel_id - if translate_to is None: - translate_to = [] - self.translate_to: list[str] = translate_to - - @staticmethod - def from_dict(source: dict): - """ - Create a new user configs from the given source - :param source: The source to create a new user - :return: The new user config - """ - temp_id = 0 - user = Channel(temp_id) - for key, value in source.items(): - setattr(user, key, value) - - return user - - def to_dict(self) -> dict: - """ - Convert this user configs to dictionary - :return: The dictionary - """ - return vars(self) +from mongo.channel import Channel def get_collection() -> AsyncCollectionReference: diff --git a/firestore/exceptions.py b/firestore/exceptions.py deleted file mode 100644 index aa3c842..0000000 --- a/firestore/exceptions.py +++ /dev/null @@ -1,12 +0,0 @@ -""" -Provides exceptions for the Discord bot - -Classes: - UserNotFoundError -""" - - -class UserNotFoundError(RuntimeError): - """ - Thrown when the user is not found - """ diff --git a/firestore/user.py b/firestore/user.py index ec66edf..0b93c15 100644 --- a/firestore/user.py +++ b/firestore/user.py @@ -1,9 +1,6 @@ """ Provides functions to interact with the user configs in the firestore -Classes: - User - Functions: get_collection() has_channel() @@ -14,42 +11,7 @@ from google.cloud.firestore_v1 import AsyncCollectionReference from firestore import db - - -class User: - """ - A wrapper class to represent user configs in the database - """ - - def __init__(self, user_id: int, chat_history_id: str = None, translate_to: list[str] = None, - main_language: str = None): - self.user_id = user_id - self.chat_history_id = chat_history_id - if translate_to is None: - translate_to = [] - self.translate_to: list[str] = translate_to - self.main_language = main_language - - @staticmethod - def from_dict(source: dict): - """ - Create a new user configs from the given source - :param source: The source to create a new user - :return: The new user config - """ - temp_user_id = 0 - user = User(temp_user_id) - for key, value in source.items(): - setattr(user, key, value) - - return user - - def to_dict(self) -> dict: - """ - Convert this user configs to dictionary - :return: The dictionary - """ - return vars(self) +from mongo.user import User def get_collection() -> AsyncCollectionReference: diff --git a/scripts/cai_tgt.py b/scripts/cai_tgt.py new file mode 100644 index 0000000..57fb9a1 --- /dev/null +++ b/scripts/cai_tgt.py @@ -0,0 +1,30 @@ +""" +This script is used to get the tgt of a character. +""" + +import asyncio +import sys + +from characterai.pyasynccai import PyAsyncCAI + + +async def main(): + """ + Main function + """ + + if len(sys.argv) != 3: + print("Usage: python cai_tgt.py ") + sys.exit(1) + + client = PyAsyncCAI(sys.argv[1]) + json = await client.character.info(sys.argv[2]) + + if "status" in json: + raise RuntimeError(json["status"]) + + print("tgt:", json["character"]["participant__user__username"]) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/scripts/clear_commands.py b/scripts/clear_commands.py index 0087e80..21b24e0 100644 --- a/scripts/clear_commands.py +++ b/scripts/clear_commands.py @@ -1,6 +1,7 @@ """ Clears all app commands of the bot """ + import os import sys