# QQ_bot **Repository Path**: wlf-darkmatter/qq_bot ## Basic Information - **Project Name**: QQ_bot - **Description**: 自建的王小美QQ机器人 - **Primary Language**: Python - **License**: MulanPSL-2.0 - **Default Branch**: gitee - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 5 - **Forks**: 0 - **Created**: 2022-03-07 - **Last Updated**: 2023-05-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 插件安装命令记录 ```bash ### 安装LOL赛事报告插件 nb plugin install nonebot-plugin-lolmatch ``` lol赛事模块 ```bash 主命令 lol 查看今日比赛信息 附带命令 本周 查看本周比赛信息 附带命令 详情 [matchID] 查询指定比赛详细信息 附带命令 订阅 [tournamentID] 订阅相关系列赛 每晚检查当日结果和第二天赛程 附带命令 赛事 查看所有即将进行或正在进行的赛事 ``` ### 涩图插件 ```bash nb plugin install nonebot_plugin_setu ``` ### 涩图发送功能 2022年3月1日 更新大量图片 ### LOL宇宙英雄故事插件 #### 配置说明 插件安装时间为 **2022年1月25日** 插件下载地址,需要手动更新 https://github.com/cjladmin/lolheroes 插件安装路径为`./GanYu/plugins/lolheroes` 在bot.py文件内添加:`nonebot.load_plugin("lolheroes")` #### 使用说明 > 直接在群内/私聊发送: >> lol`xxx`背景故事 >> 例:lol亚索/疾风剑豪背景故事 ### 虚拟聊天机器人 https://github.com/snowyfirefly/VirtualFriends ```bash pip install nonebot-plugin-vf ``` ## 开发文档 ### 自定义插件 #### wlf_plugins_debug 该模块调用了钩子函数进行测试 ```python from nonebot import get_driver driver = get_driver() # 这些钩子会在nonebot启动的时候运行 @driver.on_startup async def debug_test(): print("正在进行debug_test") debug_fun() pass # 此为需要debug测试的函数 def debug_fun: pass ``` ### bot使用 `bot.send(str)` ,这里只能调用str,不能放入Message ### 定时函数使用参考 https://apscheduler.readthedocs.io/en/3.x/userguide.html#code-examples #### “cron” ```Parameters year (int|str) – 4-digit year month (int|str) – month (1-12) day (int|str) – day of month (1-31) week (int|str) – ISO week (1-53) day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun) hour (int|str) – hour (0-23) minute (int|str) – minute (0-59) second (int|str) – second (0-59) start_date (datetime|str) – earliest possible date/time to trigger on (inclusive) end_date (datetime|str) – latest possible date/time to trigger on (inclusive) timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone) jitter (int|None) – delay the job execution by jitter seconds at most ``` #### “interval” ```Parameters weeks (int) – number of weeks to wait days (int) – number of days to wait hours (int) – number of hours to wait minutes (int) – number of minutes to wait seconds (int) – number of seconds to wait start_date (datetime|str) – starting point for the interval calculation end_date (datetime|str) – latest possible date/time to trigger on timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations jitter (int|None) – delay the job execution by jitter seconds at most ``` ### 钩子使用参考 在 `nonebot2` 中有一系列预定义的钩子函数,分为两类:`全局钩子函数` 和 `事件钩子函数` ,这些钩子函数可以用装饰器的形式来使用。 #### 全局钩子函数 全局钩子函数是指 `nonebot2` 针对其本身运行过程的钩子函数。 这些钩子函数是由其后端驱动 `driver`来运行的,故需要先获得全局 `driver` 对象: ```python from nonebot import get_driver driver=get_driver() ``` 共分为六种函数: ##### 启动准备 这个钩子函数会在 `nonebot2` 启动时运行。 ```python @driver.on_startup async def do_something(): pass ``` ##### 终止处理 这个钩子函数会在 `nonebot2` 终止时运行。 ```python @driver.on_shutdown async def do_something(): pass ``` ##### bot 连接处理 这个钩子函数会在 `bot` 通过 `websocket` 连接到 `nonebot2` 时运行。 ```python @driver.on_bot_connect async def do_something(bot: Bot): pass ``` ##### bot 断开处理 这个钩子函数会在 `bot` 断开与 `nonebot2` 的 `websocket` 连接时运行。 ```python @driver.on_bot_disconnect async def do_something(bot: Bot): pass ``` ##### bot api 调用钩子 这个钩子函数会在 `Bot` 调用 API 时运行。 ```python from nonebot.adapters import Bot @Bot.on_calling_api async def handle_api_call(bot: Bot, api: str, data: Dict[str, Any]): pass ``` ##### bot api 调用后钩子 这个钩子函数会在 `Bot` 调用 API 后运行。 ```python from nonebot.adapters import Bot @Bot.on_called_api async def handle_api_result(bot: Bot, exception: Optional[Exception], api: str, data: Dict[str, Any], result: Any): pass ``` #### 事件处理钩子 这些钩子函数指的是影响 `nonebot2` 进行 `事件处理` 的函数。 > 提示,关于 `事件处理` 的流程,可以在[这里](https://61d3d9dbcadf413fd3238e89--nonebot2.netlify.app/2.0.0a15/advanced/README)查阅 > 注意 > > 1.在事件处理钩子函数中,与 `matcher` 运行状态相关的函数将不可用,如 `matcher.finish()` > > 2.如果需要在事件处理钩子函数中打断整个对话的执行,请参考以下范例: > > ```python > from nonebot.exception import IgnoredException > > > @event_preprocessor > async def do_something(bot: Bot, event: Event, state: T_State): > raise IgnoredException("reason") > ``` 共分为四种函数: ##### 事件预处理 这个钩子函数会在 `Event` 上报到 `nonebot2` 时运行 ```python from nonebot.message import event_preprocessor @event_preprocessor async def do_something(bot: Bot, event: Event, state: T_State): pass ``` ##### 事件后处理 这个钩子函数会在 `nonebot2` 处理 `Event` 后运行 ```python from nonebot.message import event_postprocessor @event_postprocessor async def do_something(bot: Bot, event: Event, state: T_State): pass ``` ##### 运行预处理 这个钩子函数会在 `nonebot2`运行 `matcher` 前运行。 ```python from nonebot.message import run_preprocessor @run_preprocessor async def do_something(matcher: Matcher, bot: Bot, event: Event, state: T_State): pass ``` ##### 运行后处理 这个钩子函数会在 `nonebot2`运行 `matcher` 后运行。 ```python from nonebot.message import run_postprocessor @run_postprocessor async def do_something(matcher: Matcher, exception: Optional[Exception], bot: Bot, event: Event, state: T_State): pass ``` ### 直接调用cqhttp的API 基类:[`nonebot.adapters._base.Bot`](https://61d3d9dbcadf413fd3238e89--nonebot2.netlify.app/2.0.0a15/api/adapters/#nonebot.adapters._base.Bot) CQHTTP 协议 Bot 适配。继承属性参考 [BaseBot](https://61d3d9dbcadf413fd3238e89--nonebot2.netlify.app/2.0.0a15/api/adapters/#class-basebot) 。 ```python from nonebot.adapters.cqhttp import Bot Bot.call_api() ``` *async* `call_api(api, **data)` - **说明** 调用 CQHTTP 协议 API - **参数** - `api: str`: API 名称 - `**data: Any`: API 参数 - **返回** - `Any`: API 调用返回数据 - **异常** - `NetworkError`: 网络错误 - `ActionFailed`: API 调用失败 ## Docker部署 ### 本地部署 #### 基础配置安装 ##### 容器初始化 采用`ubuntu:18.04`镜像,配置成自己的喜好形式 ```bash docker pull ubuntu:18.04 docker cp ~/.bashrc ``` ##### 容器系统配置 首先进入到docker容器内 ```bash apt update apt install -y \ gcc \ g++ \ vim \ module-init-tools \ initramfs-tools \ libcupti-dev \ libxml2 \ pciutils \ software-properties-common \ dkms \ ubuntu-drivers-common \ wget \ rename \ sudo \ libgl1-mesa-glx #使用中文 apt install -y \ language-pack-zh-hans \ language-pack-zh-hans-base #-- 生成语言文件 locale-gen zh_CN.UTF-8 #写入终端启动脚本 $LANG和$LANGUAGE 分别控制语言环境和地区 cat>>~/.bashrc<