记录一次 Linux 下部署 QQ 群聊机器人

这么好玩的 Bot 为什么不弄一个呢?

Posted by Rin on August 8, 2021

记录一次 Linux 下部署 QQ 群聊机器人

本次搭建 QQ 群聊机器人会使用以下的开源项目:

服务器的系统镜像

本教程使用的是 Debian 10,理论上 Ubuntu 也适用本教程

开始部署

第一步,通过 ssh 连接上你的服务器

本教程默认使用 root 账号执行指令,你也可以创建一个拥有 sudo 权限的用户完成部署

安装运行所需环境

1
2
3
apt -y update
apt -y install build-essential
apt -y git vim

Clash(非必要)

由于我的服务器在国内,使用 Clash 可以在部署过程中涉及到外网的步骤中减少不必要的麻烦,并且在机器人的功能中也有相关服务会涉及到,所以我这里选择了安装。

Clahs 下载:https://github.com/Dreamacro/clash/releases/tag/premium

建议下载 Premium 内核 因为我是 Debian 10 所以选择 clash-linux-amd64 版本。

在国内服务器上连接 Github 速度太慢,所以建议自己本地下载,再使用 WinSCP 上传到服务器。

安装 Clash

1
2
3
4
5
6
7
8
9
10
11
gunzip clash-linux-amd64.gz
# 解压

mv clash-linux-amd64 clash 
# 重命名

chmod +x clash
# 给 clash 执行权限

mv clash /usr/bin/clash
# 将 clash 移动到 /usr/bin/clash

编辑 config.yaml 文件

一般的机场已经提供了 config.yaml 文件,只需要直接将文件通过 WinSCP 上传到服务器即可

1
2
3
4
5
mkdir /etc/clash
# 在 /etc 创建 clash

mv config.yaml /etc/clash
# 将 config.yaml 移动到 /etc/clash

将 Clash 添加到系统服务并设置自动启动

通过 vim 进行操作

1
vim /lib/systemd/system/clash.service

写入以下内容

1
2
3
4
5
6
7
8
9
10
[Unit]
Description=clash proxy
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/clash -d /etc/clash

[Install]
WantedBy=multi-user.target

运行并设置开机自启 Clash

1
2
3
4
5
systemctl start clash.service
# 启动 Clash

systemctl enable clash.service
# 设置开机自启

测试

1
2
3
4
5
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7891
# 设置 http 代理和 socks5 代理

curl -I https://www.google.com/
# 测试

出现返回结果即为成功

安装 go-cqhttp

使用 mirai 以及 MiraiGo 开发的 cqhttp golang 原生实现, 并在 cqhttp 原版 的基础上做了部分修改和拓展。 文档目前还在撰写中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mkdir go-cqhttp&&cd go-cqhttp
# 创建 go-cqhttp 文件夹井将工作路径切换至 go-cqhttp

wget https://github.com/Mrs4s/go-cqhttp/releases/download/v0.9.40-fix5/go-cqhttp_linux_amd64.tar.gz
# 下载 go-cqhttp

tar xf go-cqhttp_linux_amd64.tar.gz
# 解压

chmod +x go-cqhttp
# 给 go-cqhttp 执行权限

./go-cqhttp
# 运行 go-cqhttp

首次运行会在 go-cqhttp 目录下生成 config.hjson 文件,需要填写好作为机器人的账号与密码

1
2
3
4
// QQ号
uin: 
// QQ密码
password: ""

其余配置有详细注释,此处省略

安装 cq-picsearcher-bot

这是一个以 Node.js 编写的酷Q机器人程序,用于搜图、搜番、搜本子,并夹带了许多娱乐向功能。

首先安装 Node.js

1
2
3
4
5
6
7
# Ubuntu 用户
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs

# Debian 用户
curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
apt-get install -y nodejs

部署 cq-picsearcher-bot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
git clone https://github.com/Tsuk1ko/cq-picsearcher-bot.git --depth=1
# clone 仓库

cd cq-picsearcher-bot
# 切换工作路径

cp config.default.jsonc config.jsonc
#将 config.default.jsonc 复制为 config.jsonc

npm i -g yarn
# 使用 npm 全局安装 yarn
# 国内用户未配置 Clash 可以加上:--registry=https://registry.npm.taobao.org

yarn config set registry https://registry.npm.taobao.org --global
yarn config set disturl https://npm.taobao.org/dist --global
# 国内用户已配置 Clash 或境外服务器请忽略

yarn install
# 安装依赖

修改 config.jsonc

第二十一行的 admin 字段修改为你自己的 QQ 号

第二百三十七行的 saucenaoApiKey 字段在 Saucenao 创建账号, 并在 这里 找到你的 api key

如果安装了 Clash 建议修改第六十四行的 proxy 配置为

1
"proxy": "http://127.0.0.1:7890",

然后根据个人需求修改 config.jsonc ,配置有详细注释,此处省略

安装 Wecab

安装 MongoDB Community Edition

由于 Wecab 的数据存储方式为 MongoDB,所以这个也要装上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sudo apt-get install gnupg
#安装 gnupg

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | apt-key add -
# 导入公匙

# Debian 10 使用这条命令
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | tee /etc/apt/sources.list.d/mongodb-org-5.0.list

# Ubuntu 20.04 使用这条命令
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-5.0.list

apt-get update

apt-get install -y mongodb-org

运行并设置开机自启 MongoDB Community Edition

1
ps --no-headers -o comm 1
1
2
3
4
5
6
7
# 如上一条命令返回 systemd

systemctl start mongod
# 启动 MongoDB

systemctl enable mongod
# 设置开机自启
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 如上一条命令返回 init

service mongod start
# 启动 MongoDB

chkconfig mongod on
# 设置开机自启

# 如果报错 
chkconfig: command not found

# 则运行,再进行设置开机自启
apt install sysv-rc-conf
ln -s /usr/sbin/sysv-rc-conf /usr/sbin/chkconfig

部署 Wecab

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
git clone https://github.com/Ninzore/Wecab.git
# clone 仓库

cd Wecab
# 切换工作路径

cp config.default.json config.json
#将 config.default.jsonc 复制为 config.jsonc

npm i -g yarn
# 使用 npm 全局安装 yarn
# 国内用户未配置 Clash 可以加上:--registry=https://registry.npm.taobao.org

yarn config set registry https://registry.npm.taobao.org --global
yarn config set disturl https://npm.taobao.org/dist --global
# 国内用户已配置 Clash 或境外服务器请忽略

yarn install
# 安装依赖

修改 config.jsonc

第十五行的 admin 字段修改为你自己的 QQ 号

如果安装了 Clash 建议修改 proxy 配置

1
"proxy": "http://127.0.0.1:7890",

运行

使用 pm2 运行 go-cqhttp

1
2
3
4
5
6
7
8
9
10
11
yarn global add pm2
# 全局安装 pm2

cd ~/go-cqhttp
# 切换工作路径

./go-cqhttp
# 首次启动后可能还需要手机认证码才能登陆,所以建议启动一次

pm2 start ./go-cqhttp
# pm2 启动 go-cqhttp

运行 cq-picsearcher-bot & Wecab

1
2
3
4
5
6
7
8
9
10
11
cd ~/cq-picsearcher-bot
# 切换工作路径

npm start
# 启动 cq-picsearcher-bot

cd ~/Wecab
# 切换工作路径

npm run start
# 启动 Wecab

常用命令

go-cqhttp

1
2
3
4
5
6
7
8
pm2 restart go-cqhttp
# 重启 go-cqhttp

pm2 stop go-cqhttp
# 停止 go-cqhttp

pm2 log go-cqhttp
# 查看日志

cq-picsearcher-bot & Wecab

1
2
3
4
5
6
7
8
npm stop
# 停止

npm restart
# 重启

npm run log
# 查看日志(平时日志也会保存在 logs 文件夹内)