diff --git a/pkg/qqbot/command.py b/pkg/qqbot/command.py index 6aaceac6..7de7e5a7 100644 --- a/pkg/qqbot/command.py +++ b/pkg/qqbot/command.py @@ -11,6 +11,7 @@ import pkg.utils.reloader import pkg.utils.updater import pkg.utils.context import pkg.qqbot.message +import pkg.utils.credit as credit from mirai import Image @@ -254,6 +255,12 @@ def process_command(session_name: str, text_message: str, mgr, config, .get_image_count_of_key(api_keys[key_name]) reply_str += "{}:\n - 文本长度:{}\n - 图片数量:{}\n".format(key_name, int(text_length), int(image_count)) + # 获取此key的额度 + try: + credit_data = credit.fetch_credit_data(api_keys[key_name]) + reply_str += " - 使用额度:{:.2f}/{:.2f}\n".format(credit_data['total_used'],credit_data['total_granted']) + except Exception as e: + logging.warning("获取额度失败:{}".format(e)) reply = [reply_str] elif cmd == 'draw': diff --git a/pkg/utils/credit.py b/pkg/utils/credit.py new file mode 100644 index 00000000..f263ed9b --- /dev/null +++ b/pkg/utils/credit.py @@ -0,0 +1,13 @@ +# OpenAI账号免费额度剩余查询 +import requests + + +def fetch_credit_data(api_key: str) -> dict: + """OpenAI账号免费额度剩余查询""" + resp = requests.get( + url="https://api.openai.com/dashboard/billing/credit_grants", + headers={ + "Authorization": "Bearer {}".format(api_key), + } + ) + return resp.json() \ No newline at end of file