Giter Site home page Giter Site logo

qq-tex-bot's Introduction

qq-tex-bot

基于 mathjax 的用于渲染数学公式 (和其它一些额外功能) 的 qq 机器人; 同时支持 go-cqhttpmirai 两种 bot 框架.

功能展示

支持的命令:

  • /am: asciimath, 简易版本的公式标记语言
  • /tex: tex 公式, 较复杂, 但功能更全
  • /rotate: 旋转图片 (但自己发的图片除外). 妈妈再也不怕我得颈椎病了
  • /savepic: 保存群聊图片, 化身弔图稽器人
  • /1a2b: 老少皆宜的猜数字小游戏
  • /riddle: 猜灯谜
  • /文案: 文案生成器
  • /wdl: wordle 游戏

管理员命令:

  • /remake: 重新载入配置
  • /block <qq号>: 临时拉黑某人, bot 下次启动时拉黑自动解除
图1 图2

快速上手

go-cqhttp 和 mirai 选择一个安装即可. 推荐 go-cqhttp 因为它更稳定而且配置简单.

0. 安装 go-cqhttp

  • 下载最新的 go-cqhttp.
  • 初次运行 gocq 时选择正向 websocket 协议, gocq 会自动生成配置文件 config.yml.
  • 打开配置文件, 填写
    • account.uin: bot 的 qq 号
    • account.password: bot 的密码
    • message.post-format: 上报类型, 从 string 改为 array.

具体配置可以参考本仓库的 gocq/config.yml.

1. 安装 mirai (可选)

mirai 是全平台、开源的 qq 机器人框架, 使用 java 和 kotlin 编写. 官方用户手册

  • 确保 java 版本至少是 11

  • 新建目录 mirai, 下载 mcl-installermiral-console-loader, 然后运行

  • 安装 mirai-api-http 插件, 用于提供 http 接口:

    ./mcl --update-package net.mamoe:mirai-api-http --type plugin --channel stable-v2

    打开配置文件 config/net.mamoe.mirai-api-http/setting.yml, 在 http 协议下增加一个 ws (Drincann/Mirai-js#124):

    adapters:
      - http
      - ws
    ...
  • 运行 ./mcl, 一切正常的话, mirai-console 就会启动起来.
  • 在 mirai-console 中输入 /help 查看帮助. 这时顺便配置一下 qq 的自动登录:
    /autoLogin add <account> <password> [passwordKind]    # 添加自动登录

Trouble Shooting

  • mirai 各插件更新迭代很快, 启动 mcl 时容易出现版本不一致而报错的问题 (比如 kotlin 的 no such method 之类). 解决方法是编辑 mirai 的 config.json, 手动修改插件的 version, 然后尝试重启 ./mcl. 本仓库的 mirai/config.json 可以做一定的参考.
  • 默认情况下 mirai 以 android 协议登录, 此时不允许再用 android 手机登录同一个账号, 否则 mirai 会被强制下线. 你可以在 config/Console/AutoLogin.yml 中切换协议.
  • 如要将机器人部署到服务器, 建议先在自己电脑上登录 mirai console, 并进行滑块验证. 成功以后关闭 mirai console, 将 bots/<qq号>/device.json 文件拷贝到服务器. 这时服务器应该能顺利登录.

2. 安装 node js 依赖

假定已经安装最新版本的 node js. 在 qq-tex-bot 项目根目录 (package.json 所在的目录) 下运行

npm install

Trouble Shooting

  • 安装过程如遇到 phantomjs 下载失败, 可以手动下载到指定的目录中:
    Downloading https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-windows.zip
    Saving to C:\Users\Administrator\AppData\Local\Temp\phantomjs\phantomjs-2.1.1-windows.zip
    Receiving...
    Error making request.
    Error: connect ETIMEDOUT *.*.*.*:443
        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1148:16)
    
    比如上面这种情况, 只需到 https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-windows.zip 下载文件, 保存到 C:\Users\Administrator\AppData\Local\Temp\phantomjs\phantomjs-2.1.1-windows.zip, 然后重新运行 npm install 即可.
  • 如果始终无法装上 phantomjs, 也可以安装 image magick, 并确保 magick 命令可用, 然后, 在 bot 的 config.js 中指定 image.enginemagick (见下节).

3. 配置你的 bot

在项目根目录下新建 config.js, 填写必要信息:

module.exports = {
  // 如使用 go-cqhttp 框架, 需配置 gocq
  gocq: {
    ws: 'ws://0.0.0.0:8080/',
    qq: 123456, // 机器人的 qq 号
  },
  // 如使用 mirai, 需配置 server
  // server: {
  //   verifyKey: '', // mirai-api-http 2.x 提供的 verifyKey
  //   // authKey: '', // mirai-api-http 1.x 提供的 authKey
  //   qq: 123456, // 机器人的 qq 号
  // },
  groups: {
    112233: '群名称', // 群号和群名称
  },
  image: {
    // 图片目录绝对路径. 分隔符一律用斜杠 (/), 不要用反斜杠, 即使你是 windows
    path: '/path/to/gocq/data', // gocq
    // path: '/path/to/mirai', // mirai-api-http 2.x
    // path: '??/mirai/data/net.mamoe.mirai-api-http/images', // mirai-api-http 1.x
  },
  auth: {
    admin: [123124123], // 你的 qq 号
    blackList: [], // 黑名单
    // whiteList: [], // 白名单
    // blackGroup: [], // 群聊黑名单
    // whiteGroup: [], // 群聊白名单
  },
  // 启用的插件
  // math 和 savepic 在各个群都能使用
  // 1a2b, riddle, copywriting, mma, wordle 只在 whiteGroup 指定的群中可用
  plugins: {
    math: {},
    savepic: {
      saveGroup: []
    },
    '1a2b': {
      whiteGroup: [],
    },
    riddle: {
      whiteGroup: [],
    },
    copywriting: {
      whiteGroup: [],
    },
    mma: {
      // 配置了 whiteGroup 后,该群成员都可使用,不限于 admin。
      // 如要只允许 admin 使用,请去掉 whiteGroup
      // whiteList: config.auth.admin,
      whiteGroup: [],
      whiteList: [],
    },
    wordle {
      whiteGroup: [],
    },
  },
}
参数 解释 类型 是否必填
gocq.ws gocq 服务地址 String 默认值 ws://0.0.0.0:8080/
gocq.qq 机器人的 qq 号 Number 使用 gocq 时必填
server.baseUrl mirai 服务地址 String 默认值 http://localhost:8080
server.verifyKey mirai-api-http 提供的 verifyKey, 请妥善保存, 不要泄露 String 使用 mirai 时必填
server.qq 机器人的 qq 号 Number 使用 mirai 时必填
groups 机器人加入的群, 以 群号: 群名 的格式填写, 可填多个 Object 必填
image.path mirai 图片目录的绝对路径. 分隔符一律用斜杠 (/), 不要用反斜杠, 即使你是 windows String 必填
image.engine svg 转 png 的图片引擎, 可选 phantom 或 magick. 如果选择 magick 引擎, 还需要安装 image magick, 并保证 path 环境变量中有 magick 命令 String 默认值 phantom
image.name 临时图片的文件名 String 默认值 tmp.png
replyFriend 是否回复好友消息 Boolean 默认值 false
auth.admin 管理员们的 qq 号 Number[] 默认为空
auth.blackList 黑名单 Number[] 默认为空
auth.whiteList 白名单 Number[] 默认为空
auth.blackGroup 群聊黑名单 Number[] 默认为空
auth.whiteGroup 群聊白名单 Number[] 默认为空
plugins 开启的插件 Object 必填
plugins.math.display 公式是否为 displayStyle Boolean 默认值 true
plugins.math.ex 公式的字体大小 Number 默认值 16

最后, 运行 npm startnode index.js 启动机器人.

cli

机器人的简单命令行界面. 当你运行 npm start 后就自动进入命令模式了, 有以下指令可用:

  • /ls: 查看当前加入的所有群
  • /cd [index]: 切换到第 index 个群, 如省略 index, 则显示当前的群
  • 文字消息 \178: 发送文字消息到当前的群. \178 代表滑稽的 qq 表情. 更多表情码在这里
  • /quit <群号>: 退群.

FAQs

  • 电脑端打 /am 会变成表情怎么办?

    解决方法1: 右键单击输入框, 关闭表情快捷键

    解决方法2: 使用 /AM

  • 如何编写多行文字 / 公式?

    更新: /am 现在直接支持多行公式:

    /am
    "第一行"
    "第二行"
    

    可以用 gather 环境

    /tex
    \begin{gather}
    x^2 \\ y^2
    \end{gather}
    

    或者 \displaylines:

    /tex
    \displaylines{ x=a+b \\ y=b+c }
    

    或者用矩阵:

    /am
    {: "第一行"; "第二行" :}
    

TODO

qq-tex-bot's People

Contributors

zmx0142857 avatar cc050511 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.