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

Commit

Permalink
Merge Release 1.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
winstonsung authored Apr 22, 2021
2 parents 076557b + c6ae1c6 commit 1107d2c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discord-qq-bridge",
"version": "1.5.0",
"version": "1.5.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion src/bridge-discord-to-qq.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {handlerAt, parseEmoji} from './bridge-discord-to-qq';
import { handlerAt, handlerAtQQUser, parseEmoji } from './bridge-discord-to-qq';

const fakeMessage: any = {msg: {mentions: {users: [{id: '23332333323333', username: '张三', discriminator: '0000'}]}}};

Expand All @@ -11,4 +11,8 @@ describe('bridge-bridge-to-qq.spec.ts', () => {
it('handlerAt', async () => {
expect(await handlerAt(`<@!23332333323333>`, fakeMessage)).toBe(`@张三#0000`)
});
it('handlerAtQQ', async () => {
expect(await handlerAtQQUser(`@零の幻影(1244321579)`, fakeMessage)).toBe(`[CQ:at,qq=1244321579]`)
expect(await handlerAtQQUser(`@兔子(rabbitkiller)(243249439)`, fakeMessage)).toBe(`[CQ:at,qq=243249439]`)
});
});
45 changes: 4 additions & 41 deletions src/bridge-discord-to-qq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export async function handlerAt(message: string, ctx: { msg: Message, bridge: Br

// 处理at discord用户
export async function handlerAtQQUser(message: string, ctx: { msg: Message, bridge: BridgeConfig }): Promise<string> {
const atList: Array<{ username: string, qq?: string, origin: string }> = [];
const atList: Array<{ username: string, qq: string, origin: string }> = [];
// 正则匹配
const m1 = message.match(/\@([^\n]+) (?:\()([0-9]+)\)(\#0000)?/g);
if (m1) {
Expand All @@ -188,49 +188,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: GroupMemberInfo[] = await koishi.bots[0].getGroupMemberList(ctx.bridge.qqGroup);
fetchedMembers.forEach((member) => {
// 匹配用户名
const ats = atList.filter(at => {
if (at.qq && parseInt(at.qq) === member.userId) {
return true;
} else if (at.qq) {
return false;
}
return at.username === member.card || at.username === member.nickname;
});
if (ats.length === 0) {
return;
}
// 替换
ats.forEach((at) => {
message = message.replace(at.origin, CQCode.stringify('at', {qq: member.userId}))
})
});
atList.forEach((at)=>{
message = message.replace(at.origin, CQCode.stringify('at', {qq: at.qq}))
})
return message;
}

Expand Down

0 comments on commit 1107d2c

Please sign in to comment.