diff options
author | Kyomotoi <[email protected]> | 2023-01-31 12:02:34 +0800 |
---|---|---|
committer | Kyomotoi <[email protected]> | 2023-01-31 12:02:34 +0800 |
commit | e1b649615a2f91b644d08351d17572a29378637c (patch) | |
tree | 89e65affc81602e5c3399275a1e012a472872877 /ATRI/configs | |
parent | f44f9e148c0e5fc563c057efe923e2d41a63c27d (diff) | |
download | ATRI-e1b649615a2f91b644d08351d17572a29378637c.tar.gz ATRI-e1b649615a2f91b644d08351d17572a29378637c.tar.bz2 ATRI-e1b649615a2f91b644d08351d17572a29378637c.zip |
⚡️ 一键化设置
Diffstat (limited to 'ATRI/configs')
-rw-r--r-- | ATRI/configs/config.py | 15 | ||||
-rw-r--r-- | ATRI/configs/create.py | 115 | ||||
-rw-r--r-- | ATRI/configs/data_source.py | 47 | ||||
-rw-r--r-- | ATRI/configs/default_config.yml | 22 |
4 files changed, 176 insertions, 23 deletions
diff --git a/ATRI/configs/config.py b/ATRI/configs/config.py index 39e1e13..cbd1705 100644 --- a/ATRI/configs/config.py +++ b/ATRI/configs/config.py @@ -2,6 +2,7 @@ import yaml from time import sleep from pathlib import Path +from .create import init_config from .models import BotConfig, ConfigModel, WithGoCQHTTP, RuntimeConfig @@ -14,18 +15,8 @@ _DEFAULT_CONFIG_PATH = Path(".") / "ATRI" / "configs" / "default_config.yml" class Config: def __init__(self, config_path: Path): if not config_path.is_file(): - try: - with open(config_path, "w", encoding="utf-8") as w: - w.write(_DEFAULT_CONFIG_PATH.read_text("utf-8")) - - print("[!] 未检测到 config.yml, 已于当前目录生成, 请参照文档进行填写并重新启动") - print("[!] 文档地址: https://atri.imki.moe/install/configuration-bot/") - sleep(3) - exit(0) - except Exception as err: - print(f"[x] 写入文件 config.yml 失败, 请确认是否给足权限: {err}") - sleep(3) - exit(-1) + init_config(config_path, _DEFAULT_CONFIG_PATH) + sleep(3) else: with open(config_path, "r", encoding="utf-8") as r: if "BotSelfConfig" in r.read(): diff --git a/ATRI/configs/create.py b/ATRI/configs/create.py new file mode 100644 index 0000000..84ca9a3 --- /dev/null +++ b/ATRI/configs/create.py @@ -0,0 +1,115 @@ +from pathlib import Path +from ipaddress import IPv4Address + +from .data_source import Console, C + + +console = Console(C()) + + +def init_config(conf_path: Path, default_conf_path: Path): + console.print("\n[b]アトリは、高性能ですから![/b]\n", style="#00bee6") + + console.warn("文档地址: https://atri.imki.moe") + console.info("这是你第一次启动本项目, 请根据提示填写信息") + console.info("如中途填写错, 你可以直接退出, 再次启动以重新填写") + console.info("如需使用默认值, Enter 跳过方可\n") + + console.info("[b]Bot 主体设置[/b]\n", style="white") + host = console.input( + "Bot 监听的主机名 (IP). 如有控制台相关需求, 建议: [green]0.0.0.0[/green] (默认: [green]127.0.0.1[/green])", + "127.0.0.1", + IPv4Address, + "输入不正确 示例: 127.0.0.1", + ) + port = console.input( + "Bot 对外开放的端口 (Port). 范围建议: [green]10000-60000[/green] (默认: [green]20000[/green])", + "20000", + int, + "输入不正确 示例: 20000", + ) + superusers = console.input( + "超级用户 (qq号), 即 Bot 的[b]主人[/b]. 可填多个, 用英文逗号 (,) 隔开 (默认: [green]1145141919[/green])", + "1145141919810", + str, + "输入不正确 示例: 1145141919" + ) + access_token = console.input( + "协议端通信密钥, 此项留空[b]将无法进入控制台[/b]. 无长度限制 示例: [green]21^sASDA!@3l67GJlk7sd!14#[/green]", + str(), + str, + "输入不正确 示例: 21^sASDA!@3l67GJlk7sd!14# (请尽可能复杂, 无长度限制)" + ) + proxy = console.input( + "是否有代理. 格式参考: http(s)://127.0.0.1:8100 (如无请 Enter 以跳过)", + str(), + str, + "输入不正确 示例: http://127.0.0.1:8100" + ) + console.success("Bot 主体配置完成\n") + + console.info("[b]Bot 进阶设置[/b]\n", style="white") + is_use_with_gocq = console.input( + "是否启用内置的 gocqhttp? (y/n) (默认: y)", + "y", + str, + "输入不正确 示例: y" + ) + if is_use_with_gocq in ["y", "Y", "true", "True", "是"]: + uin = console.input( + "Bot 账号", + str(), + int, + "输入不正确 示例: 1145141919" + ) + password = console.input( + "Bot 账号密码 (已做隐藏处理, 如不确定是否填写正确, 请查阅填写完毕后所生成的文件: config.yml)", + str(), + str, + password=True + ) + protocol = console.input( + "Bot 登录设备类型. 范围: 0-5, 具体请参考文档: 部署项目-设置 (默认: 5)", + "5", + int, + "输入不正确 范围 0-5 示例: 5" + ) + download_domain = console.input( + "gocqhttp 下载源域名设置. 具体请参考文档: 部署项目-设置 (默认: github.com)", + "github.com" + ) + is_use_with_gocq = True + console.success("内置 gocqhttp 配置完成. 支持多账号, 具体请参考文档: 部署项目-设置\n") + else: + is_use_with_gocq = False + console.info("已跳过\n") + + console.info("[b]Bot 插件设置[/b]\n", style="white") + saucenao_key = console.input( + "SauceNAO 搜图密钥, 如不填写将无法启用[b]以图搜图[/b], 前往官网以获取: https://saucenao.com/ (默认: 空)", + str(), + str + ) + + console.success("[white]至此, 所需基本配置已填写完毕[white]") + + raw_conf = default_conf_path.read_text("utf-8") + raw_conf = raw_conf.replace("{host}", host) + raw_conf = raw_conf.replace("{port}", port) + raw_conf = raw_conf.replace("{superusers}", superusers) + raw_conf = raw_conf.replace("{access_token}", access_token) + raw_conf = raw_conf.replace("{proxy}", proxy) + + if is_use_with_gocq: + raw_conf = raw_conf.replace("{is_use_with_gocq}", "true") + raw_conf = raw_conf.replace("{uin}", str(uin)) # type: ignore + raw_conf = raw_conf.replace("{password}", password) # type: ignore + raw_conf = raw_conf.replace("{protocol}", str(protocol)) # type: ignore + raw_conf = raw_conf.replace("{download_domain}", download_domain) # type: ignore + else: + raw_conf = raw_conf.replace("{is_use_with_gocq}", "false") + + raw_conf = raw_conf.replace("{saucenao_key}", saucenao_key) + + with open(conf_path, "w", encoding="utf-8") as w: + w.write(raw_conf) diff --git a/ATRI/configs/data_source.py b/ATRI/configs/data_source.py new file mode 100644 index 0000000..8794223 --- /dev/null +++ b/ATRI/configs/data_source.py @@ -0,0 +1,47 @@ +from rich.console import Console as C + + +class Console(C): + def __init__(self, console: C, *args, **kwargs): + super().__init__(*args, **kwargs) + self.console = console + + def info(self, content: str, *args, **kwargs): + text = "[blue][¡][/blue] " + content + self.console.print(text, *args, **kwargs) + + def success(self, content: str, *args, **kwargs): + text = "[green][√][/green] " + content + self.console.print(text, *args, **kwargs) + + def warn(self, content: str, *args, **kwargs): + text = "[yellow][!][/yellow] " + content + self.console.print(text, *args, **kwargs) + + def error(self, content: str, *args, **kwargs): + text = "[red][×][/red] " + content + self.console.print(text, *args, **kwargs) + + def input( + self, + prompt: str, + default_return=str(), + assign_type=None, + reject_message=str(), + *args, + **kwargs, + ) -> str: + self.console.print(f"[gray][?][/gray] [white]{prompt}[/white]") + while True: + text = self.console.input("> ", *args, **kwargs) + if not text: + self.info(f"已使用默认设置: {default_return if default_return else '空'}") + return default_return + + if not assign_type: + return text + + try: + return assign_type(text) + except Exception: + self.warn(reject_message) diff --git a/ATRI/configs/default_config.yml b/ATRI/configs/default_config.yml index 6a423e8..7d08f5d 100644 --- a/ATRI/configs/default_config.yml +++ b/ATRI/configs/default_config.yml @@ -2,33 +2,33 @@ ConfigVersion: "1.2.0" BotConfig: - host: "127.0.0.1" - port: 20000 + host: "{host}" + port: {port} debug: false - superusers: ["1234567890"] + superusers: [{superusers}] nickname: ["ATRI", "Atri", "atri", "亚托莉", "アトリ"] command_start: ["", "/"] command_sep: ["."] session_expire_timeout: 60 access_token: "" - proxy: "" + proxy: "{proxy}" request_timeout: 5 WithGoCQHTTP: - enabled: false + enabled: {is_use_with_gocq} accounts: - - uin: 1234567890 - password: "" - protocol: 5 + - uin: {uin} + password: "{password}" + protocol: {protocol} - download_domain: "github.com" - download_version: "v1.0.0-rc1" + download_domain: "{download_domain}" + download_version: "v1.0.0-rc3" gocq_webui_username: "ATRI" gocq_webui_password: "ATRI1314000***" SauceNAO: - key: "" + key: "{saucenao_key}" Setu: reverse_proxy: true |