Skip to content

Commit

Permalink
feat: icqq 群临时
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Oct 31, 2024
1 parent e83a464 commit ba861b8
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 44 deletions.
2 changes: 1 addition & 1 deletion main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"elysia": "^1.1.23",
"esbuild": "^0.24.0",
"eviltransform": "^0.2.2",
"file-type": "19.6.0",
"file-type": "19.3.0",
"fluent-ffmpeg": "^2.1.3",
"image-size": "^1.1.1",
"lodash": "^4.17.21",
Expand Down
1 change: 1 addition & 0 deletions main/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ model Message {
model ForwardPair {
id Int @id @default(autoincrement())
qqRoomId BigInt @db.BigInt
qqFromGroupId BigInt? @db.BigInt // 用于群临时
tgChatId BigInt @db.BigInt
avatarCache AvatarCache[]
instanceId Int @default(0)
Expand Down
2 changes: 2 additions & 0 deletions main/src/api/richHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export default new Elysia()
const user = await member.client.pickFriend(member.uid) as NapCatFriend;
const info = await user.renew();
profile = {
// @ts-ignore
birthday: [info.birthday_year, info.birthday_month, info.birthday_day],
// @ts-ignore
email: info.eMail,
nickname: info.nickname,
// @ts-ignore
Expand Down
16 changes: 13 additions & 3 deletions main/src/client/OicqClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
FriendIncreaseEvent as OicqFriendIncreaseEvent,
FriendRecallEvent,
GroupRecallEvent,
FriendPokeEvent, GroupPokeEvent, MessageElem, FriendRequestEvent, GroupInviteEvent, type ImageElem, XmlElem,
FriendPokeEvent, GroupPokeEvent, MessageElem, FriendRequestEvent, GroupInviteEvent, type ImageElem, XmlElem, Group, Member,
} from '@icqqjs/icqq';
import random from '../utils/random';
import fs from 'fs';
Expand Down Expand Up @@ -170,10 +170,16 @@ export default class OicqClient extends QQClient {

private onMessage = async (event: PrivateMessageEvent | GroupMessageEvent | DiscussMessageEvent) => {
if (event.message_type === 'discuss') return;
this.log.debug('OicqClient.onMessage', event);

let chat: Friend | Group | Member = 'group' in event ? event.group : event.friend;
if (event.message_type === 'private' && event.sender.group_id) {
chat = this.oicq.pickMember(event.sender.group_id, event.sender.user_id);
}

const gEvent = new MessageEvent(
{ id: event.sender.user_id, name: ('card' in event.sender && event.sender.card) || event.sender.nickname },
'group' in event ? event.group : event.friend,
chat as any,
event.message,
event.seq,
event.rand,
Expand All @@ -185,6 +191,7 @@ export default class OicqClient extends QQClient {
event.message_id,
'atme' in event ? event.atme : false,
'atall' in event ? event.atall : false,
event.message_type === 'private' ? event.sender.group_id : undefined,
);
await this.callHandlers(this.onMessageHandlers, gEvent);
};
Expand Down Expand Up @@ -289,7 +296,10 @@ export default class OicqClient extends QQClient {
};
}

async pickFriend(uin: number) {
async pickFriend(uin: number, tempChatFromGroupId?: number) {
if (tempChatFromGroupId) {
return this.oicq.pickMember(tempChatFromGroupId, uin) as any;
}
return this.oicq.pickFriend(uin);
}

Expand Down
1 change: 1 addition & 0 deletions main/src/client/QQClient/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class MessageEvent extends ChatEvent {
public readonly messageId: string,
public readonly atMe: boolean,
public readonly atAll: boolean,
public readonly tempChatFromGroupId?: number,
) {
super(chat);
}
Expand Down
6 changes: 3 additions & 3 deletions main/src/client/QQClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,16 @@ export abstract class QQClient {

// End Handlers

public getChat(roomId: number): Promise<Group | Friend> {
public getChat(roomId: number, tempChatFromGroupId?: number): Promise<Group | Friend> {
if (roomId > 0) {
return this.pickFriend(roomId);
return this.pickFriend(roomId, tempChatFromGroupId);
}
else {
return this.pickGroup(-roomId);
}
}

public abstract pickFriend(uin: number): Promise<Friend>;
public abstract pickFriend(uin: number, tempChatFromGroupId?: number): Promise<Friend>;

public abstract pickGroup(groupId: number): Promise<Group>;

Expand Down
2 changes: 1 addition & 1 deletion main/src/controllers/ConfigController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default class ConfigController {
return false;
}
// 有未创建转发群的新私聊消息时自动创建
promise = this.configService.createGroupAndLink(chat.uid, chat.remark || chat.nickname, true);
promise = this.configService.createGroupAndLink(chat, chat.remark || chat.nickname, true, undefined, message.tempChatFromGroupId);
this.createPrivateMessageGroupBlockList.set(chat.uid, promise);
await promise;
return false;
Expand Down
8 changes: 5 additions & 3 deletions main/src/models/ForwardPairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ export default class ForwardPairs {
});
for (const i of dbValues) {
try {
const qq = await oicq.getChat(Number(i.qqRoomId));
const qq = await oicq.getChat(Number(i.qqRoomId), i.qqFromGroupId ? Number(i.qqFromGroupId) : undefined);
const tg = await tgBot.getChat(Number(i.tgChatId));
const tgUserChat = await tgUser.getChat(Number(i.tgChatId));
if (qq && tg && tgUserChat) {
this.log.debug('初始化', { qq, tg, tgUserChat });
this.pairs.push(new Pair(qq, tg, tgUserChat, i.id, i.flags, i.apiKey, oicq));
}
}
Expand All @@ -43,12 +44,13 @@ export default class ForwardPairs {
return instance;
}

public async add(qq: Friend | Group, tg: TelegramChat, tgUser: TelegramChat, qqClient: QQClient) {
public async add(qq: Friend | Group, tg: TelegramChat, tgUser: TelegramChat, qqClient: QQClient, qqFromGroupId?: number) {
const dbEntry = await db.forwardPair.create({
data: {
qqRoomId: 'uid' in qq ? qq.uid : -qq.gid,
tgChatId: Number(tg.id),
instanceId: this.instanceId,
qqFromGroupId,
},
});
this.pairs.push(new Pair(qq, tg, tgUser, dbEntry.id, dbEntry.flags, dbEntry.apiKey, qqClient));
Expand Down Expand Up @@ -81,7 +83,7 @@ export default class ForwardPairs {
public async initMapInstance(instances: Instance[]) {
for (const forwardPair of this.pairs) {
for (const instance of instances) {
if(!(instance.oicq instanceof OicqClient)) continue;
if (!(instance.oicq instanceof OicqClient)) continue;
const instanceTgUserId = instance.userMe.id.toString();
if (forwardPair.instanceMapForTg[instanceTgUserId]) continue;
try {
Expand Down
13 changes: 10 additions & 3 deletions main/src/services/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import getAboutText from '../utils/getAboutText';
import random from '../utils/random';
import { Friend, Group, QQClient } from '../client/QQClient';
import posthog from '../models/posthog';
import OicqClient from '../client/OicqClient';

const DEFAULT_FILTER_ID = 114; // 514

Expand Down Expand Up @@ -116,10 +117,10 @@ export default class ConfigService {
* @param status 传入 false 的话就不显示状态信息,可以传入一条已有消息覆盖
* @param chat
*/
public async createGroupAndLink(room: number | Friend | Group, title?: string, status: boolean | Api.Message = true, chat?: TelegramChat) {
public async createGroupAndLink(room: number | Friend | Group, title?: string, status: boolean | Api.Message = true, chat?: TelegramChat, qqFromGroupId?: number) {
this.log.info(`创建群组并关联:${room}`);
if (typeof room === 'number') {
room = await this.oicq.getChat(room);
room = await this.oicq.getChat(room, qqFromGroupId);
}
if (!title) {
// TS 这边不太智能
Expand All @@ -130,6 +131,11 @@ export default class ConfigService {
title = room.name;
}
}
if (!title && this.oicq instanceof OicqClient && 'uid' in room && qqFromGroupId) {
// 可能是群临时
const info = await this.oicq.oicq.getGroupMemberInfo(qqFromGroupId, room.uid);
title = info.card || info.nickname;
}
let isFinish = false;
try {
let errorMessage = '';
Expand Down Expand Up @@ -190,7 +196,8 @@ export default class ConfigService {
// 关联写入数据库
const chatForBot = await this.tgBot.getChat(chat.id);
status && await status.edit({ text: '正在写数据库…' });
const dbPair = await this.instance.forwardPairs.add(room, chatForBot, chat, this.oicq);
this.log.debug('正在写数据库:', room, chatForBot, chat, this.oicq, qqFromGroupId);
const dbPair = await this.instance.forwardPairs.add(room, chatForBot, chat, this.oicq, qqFromGroupId);
isFinish = true;

// 更新头像
Expand Down
39 changes: 9 additions & 30 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ba861b8

Please sign in to comment.