Skip to content
This repository has been archived by the owner on Mar 15, 2023. It is now read-only.

Commit

Permalink
fix: 修复qq回复没法同步至discord
Browse files Browse the repository at this point in the history
  • Loading branch information
yefu24324 committed May 5, 2021
1 parent bf372c1 commit 2116fac
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 60 deletions.
34 changes: 19 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@

## 前置说明
### 关于QQ机器人
想要使用QQ机器人,首先需要安装go-cqhttp。
- [go-cqhttp](https://github.com/Mrs4s/go-cqhttp):是一个用来连接QQ并且会将消息通过http或websocket的方式上报给koishi程序。以达到让程序接收消息和发送消息
- [el-bot](https://www.npmjs.com/package/koishi):是一个接入类似go-cqhttp平台的一个机器人nodejs库,用来方便我们使用nodejs制作qq机器人
QQ机器人使用了el-bot的js库
- [mirai-console-loader](https://github.com/iTXTech/mirai-console-loader) 帮助你搭建mirai所需要的环境
- [el-bot](https://docs.bot.elpsy.cn):是一个接入mirai平台的一个机器人nodejs库,用来方便我们使用nodejs制作qq机器人

### 关于Discord机器人
Discord制作机器人不需要类似go-cqhttp的中转程序。官方已经提供了相关api和开发者平台,让开发人员方便的制作机器人

使用[discord.js](https://www.npmjs.com/package/discord.js)库就可以方便的使用
使用[discord.js](https://www.npmjs.com/package/discord.js) 库就可以方便的使用

## 本库安装使用方式
### 一、下载安装go-cqhttp
[详细步骤](https://github.com/Mrs4s/go-cqhttp/blob/master/docs/quick_start.md)
### 一、启动MCL (mirai一键安装环境工具)
> 使用Docker的方式
1. 修改文件 `mcl-1.1.0-beta.1/config/Console/AutoLogin.yml` 添加属于你的qq账号
2. 直接运行命令 `docker-compose up`
正常情况,bot收到消息后,控制台会看的到就成功了

1. 将对应操作系统的go-cqhttp下载到go-cqhttp目录
2. 将go-cqhttp/config.sample.json 复制拷贝成 config.json 并配置
```shell script
"uin": 0, <--- qq号
"password": "", <--- 密码
```
3. 启动go-cqhttp
> 非Docker的方式
1. 安装java jdk 并且11以上的版本,配置好java环境变量, 控制台输入`java --version` 能看到版本信息就正常
2. 修改文件 `mcl-1.1.0-beta.1/config/Console/AutoLogin.yml` 添加属于你的qq账号
3. 进入`mcl-1.1.0-beta.1`目录,运行`./mcl`
正常情况,bot收到消息后,控制台会看的到就成功了

> 注: 推荐使用docker的方式,不只是本地,部署到云环境也方便
### 二、配置
将koishi.sample.ts 复制拷贝成 koishi.config.ts, 并配置下面几项
Expand Down Expand Up @@ -54,7 +58,7 @@ discordBotToken: '',
### 三、运行
```shell script
npm install
npm start
npm run start:dev
```

## 支持功能
Expand All @@ -65,7 +69,7 @@ npm start

### Discord -> QQ
- [x] 回复消息同步至Discord
- [ ] 支持图片和gif消息同步至Discord (gif暂不支持)
- [x] 支持图片和gif消息同步至Discord
- [x] 支持回复消息同步至Discord

## 文档相关
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
"@nestjs/core": "^7.6.13",
"@nestjs/platform-express": "^7.6.13",
"@nestjs/typeorm": "^7.1.5",
"@octokit/webhooks": "^9.4.0",
"@octokit/webhooks-definitions": "^3.67.3",
"axios": "^0.21.0",
"canvas": "^2.6.1",
"dayjs": "^1.9.6",
"discord.js": "^12.5.3",
"el-bot": "^0.7.0-alpha.2",
"el-bot": "^0.8.0-beta.5",
"fast-xml-parser": "^3.17.6",
"file-type": "^16.0.1",
"got": "^11.8.1",
Expand Down
43 changes: 3 additions & 40 deletions src/bridge-discord-to-qq.el.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,49 +217,12 @@ export async function handlerAtQQUser(message: string, ctx: { msg: Message, brid
})
})
}
[
/\[@([\w-_\s]+)\]/, // [@rabbitkiller]
/`@([\w-_\s]+)`/, // `@rabbitkiller`
].forEach((reg) => {
const gReg = new RegExp(reg.source, 'g');
const sReg = new RegExp(reg.source);
// 全局匹配满足条件的
const strList = message.match(gReg);
if (!strList) {
return;
}
strList.forEach((str) => {
// 获取用户名, 保留origin匹配上的字段用来replace
if (str.match(sReg)[1]) {
atList.push(
{origin: str, username: str.match(sReg)[1].trim()}
)
}
})
});
if (atList.length === 0) {
return message;
}
// @ts-ignore
const fetchedMembers: any[] = await BotService.qqBot.mirai.api.memberList(ctx.bridge.qqGroup);
fetchedMembers.forEach((member: {id: number, memberName: string}) => {
// 匹配用户名
const ats = atList.filter(at => {
if (at.qq && parseInt(at.qq) === member.id) {
return true;
} else if (at.qq) {
return false;
}
return at.username === member.memberName;
});
if (ats.length === 0) {
return;
}
// 替换
ats.forEach((at) => {
message = message.replace(at.origin, CQCode.stringify('at', {qq: member.id}))
})
});
atList.forEach((at)=>{
message = message.replace(at.origin, CQCode.stringify('at', {qq: at.qq}))
})
return message;
}

Expand Down
8 changes: 4 additions & 4 deletions src/bridge-qq-to-discord.el.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import config from "./el.config";
import {MessageType} from "mirai-ts";
import { Config as MiraiConfig, MessageType } from "mirai-ts";
import {Guild, Message, Message as DiscordMessage, MessageAttachment, Webhook, WebhookMessageOptions} from "discord.js";
import {BotService} from "./el-bot/bot.service";
import {downloadQQImage} from "./utils/download-file";
Expand Down Expand Up @@ -97,8 +97,8 @@ async function toDiscord(qqMsg: MessageType.GroupMessage) {

// 处理回复消息
async function handlerForward(quoteMsg: MessageType.Quote): Promise<string> {
const memberInfo = await BotService.qqBot.mirai.api.memberInfo(quoteMsg.groupId, quoteMsg.senderId);
let messageContent = `** 回复 @${memberInfo.name} 在 {获取时间失败} 的消息 **\n`;
const memberInfo = await BotService.qqBot.mirai.api.memberInfo(quoteMsg.groupId, quoteMsg.senderId) as MiraiConfig.MemberInfo;
let messageContent = `** 回复 @${memberInfo.name} 在 {暂无日期} 的消息 **\n`;
for (const msg of quoteMsg.origin) {
switch (msg.type) {
case 'Source':
Expand All @@ -109,7 +109,7 @@ async function handlerForward(quoteMsg: MessageType.Quote): Promise<string> {
messageContent += msg.text;
break;
case 'At':
const memberInfo = await BotService.qqBot.mirai.api.memberInfo(quoteMsg.groupId, msg.target);
const memberInfo = await BotService.qqBot.mirai.api.memberInfo(quoteMsg.groupId, msg.target) as MiraiConfig.MemberInfo;;
messageContent += `\`@${memberInfo.name}(${msg.target})\``;
break;
case 'AtAll':
Expand Down

0 comments on commit 2116fac

Please sign in to comment.