feat(event): refactor guild-request/guild-member-request/guild-member-removed events

This commit is contained in:
Il Harper 2024-09-14 18:21:54 +08:00
parent a5994eb876
commit fe48309be0
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
4 changed files with 336 additions and 40 deletions

View File

@ -2,7 +2,9 @@ import type { Group, Profile } from '@chronocat/red'
export const requestMethodMap: Record<string, string> = {}
export const emittedBuddyReqList: string[] = []
export const emittedGroupReqList: string[] = []
export const emittedGuildRequestList: string[] = []
export const emittedGuildMemberRequestList: string[] = []
export const emittedGuildMemberRemovedList: string[] = []
export const sendQueue: string[] = []

View File

@ -23,7 +23,9 @@ import type { ChronocatContext } from '@chronocat/shell'
import type { IpcManData } from 'ipcman'
import {
emittedBuddyReqList,
emittedGroupReqList,
emittedGuildMemberRemovedList,
emittedGuildMemberRequestList,
emittedGuildRequestList,
friendMap,
groupMap,
requestMethodMap,
@ -31,6 +33,8 @@ import {
} from './globalVars'
import {
FriendRequestDispatchMessage,
GuildMemberRemovedDispatchMessage,
GuildMemberRequestDispatchMessage,
GuildRequestDispatchMessage,
MessageCreatedDispatchMessage,
MessageDeletedDispatchMessage,
@ -219,20 +223,71 @@ const dispatcher = async (
// eslint-disable-next-line @typescript-eslint/no-misused-promises
notifies.forEach(async (x) => {
if ((x.type !== 1 && x.type !== 7) || x.status !== 1) return
switch (x.type) {
case 1: {
// guild-request
if (x.status !== 1) return
const uin = await ctx.chronocat.uix.getUin2(x.user1.uid) // 此时用户刚刚申请入群,不在群里,不能带 group 场景
if (!uin) {
ctx.chronocat.l.error('内部错误', { code: 2152 })
return
const uin = await ctx.chronocat.uix.getUin2(x.user2.uid)
if (!uin) {
ctx.chronocat.l.error('内部错误', { code: 2152 })
return
}
const key = `${x.group.groupCode}:${uin}:${x.seq}`
if (emittedGuildRequestList.includes(key)) return
emittedGuildRequestList.push(key)
ctx.chronocat.emit(new GuildRequestDispatchMessage(x, uin))
return
}
case 7: {
// guild-member-request
if (x.status !== 1) return
const uin = await ctx.chronocat.uix.getUin2(x.user1.uid)
if (!uin) {
ctx.chronocat.l.error('内部错误', { code: 2152 })
return
}
const key = `${x.group.groupCode}:${uin}:${x.seq}`
if (emittedGuildMemberRequestList.includes(key)) return
emittedGuildMemberRequestList.push(key)
// x.actionUser 此时一定为空
ctx.chronocat.emit(new GuildMemberRequestDispatchMessage(x, uin))
return
}
case 11: {
// guild-member-removed
const uin = await ctx.chronocat.uix.getUin2(x.user1.uid)
if (!uin) {
ctx.chronocat.l.error('内部错误', { code: 2152 })
return
}
const key = `${x.group.groupCode}:${uin}:${x.seq}`
if (emittedGuildMemberRemovedList.includes(key)) return
emittedGuildMemberRemovedList.push(key)
// x.actionUser 此时一定为空
ctx.chronocat.emit(new GuildMemberRemovedDispatchMessage(x, uin))
return
}
// case 8: {
// return
// }
}
const key = `${x.group.groupCode}:${uin}:${x.seq}`
if (emittedGroupReqList.includes(key)) return
emittedGroupReqList.push(key)
ctx.chronocat.emit(new GuildRequestDispatchMessage(x, uin))
})
return

View File

@ -1,4 +1,10 @@
import type { BuddyReq, GroupNotify, RedMessage } from '@chronocat/red'
import type {
BuddyReq,
GroupNotifyGuildMemberRemoved,
GroupNotifyGuildMemberRequest,
GroupNotifyGuildRequest,
RedMessage,
} from '@chronocat/red'
import type {
ChronocatContext,
ChronocatLogCurrentConfig,
@ -40,7 +46,7 @@ export class MessageDeletedDispatchMessage implements SatoriDispatchMessage {
export class GuildRequestDispatchMessage implements SatoriDispatchMessage {
constructor(
private notify: GroupNotify,
private notify: GroupNotifyGuildRequest,
private uin: string,
) {}
@ -69,7 +75,101 @@ export class GuildRequestDispatchMessage implements SatoriDispatchMessage {
user: {
id: `${this.uin}`,
name: this.notify.user1.nickName,
name: this.notify.user2.nickName,
avatar: `http://thirdqq.qlogo.cn/headimg_dl?dst_uin=${this.uin}&spec=640`,
},
message: {
id: undefined as unknown as string,
content: this.notify.postscript,
},
}
return [event]
}
}
export class GuildMemberRequestDispatchMessage
implements SatoriDispatchMessage
{
constructor(
private notify: GroupNotifyGuildMemberRequest,
private uin: string,
) {}
type = 'satori' as const
toSatori = async (
ctx: ChronocatContext,
_config: O.Intersect<
ChronocatLogCurrentConfig,
ChronocatSatoriEventsConfig
>,
) => {
const event: Event = {
id: undefined as unknown as number,
type: 'guild-member-request',
platform: ctx.chronocat.platform,
self_id: undefined as unknown as string,
timestamp: new Date().getTime(),
guild: {
id: this.notify.group.groupCode,
name: this.notify.group.groupName,
avatar: `https://p.qlogo.cn/gh/${this.notify.group.groupCode}/${this.notify.group.groupCode}/640`,
},
user: {
id: `${this.uin}`,
name: this.notify.user2.nickName,
avatar: `http://thirdqq.qlogo.cn/headimg_dl?dst_uin=${this.uin}&spec=640`,
},
message: {
id: undefined as unknown as string,
content: this.notify.postscript,
},
}
return [event]
}
}
export class GuildMemberRemovedDispatchMessage
implements SatoriDispatchMessage
{
constructor(
private notify: GroupNotifyGuildMemberRemoved,
private uin: string,
) {}
type = 'satori' as const
toSatori = async (
ctx: ChronocatContext,
_config: O.Intersect<
ChronocatLogCurrentConfig,
ChronocatSatoriEventsConfig
>,
) => {
const event: Event = {
id: undefined as unknown as number,
type: 'guild-member-removed',
platform: ctx.chronocat.platform,
self_id: undefined as unknown as string,
timestamp: new Date().getTime(),
guild: {
id: this.notify.group.groupCode,
name: this.notify.group.groupName,
avatar: `https://p.qlogo.cn/gh/${this.notify.group.groupCode}/${this.notify.group.groupCode}/640`,
},
user: {
id: `${this.uin}`,
name: this.notify.user2.nickName,
avatar: `http://thirdqq.qlogo.cn/headimg_dl?dst_uin=${this.uin}&spec=640`,
},

View File

@ -216,39 +216,30 @@ export interface OnGroupSingleScreenNotifies {
notifies: GroupNotify[]
}
export interface GroupNotify {
export type GroupNotify =
| GroupNotifyGuildRequest
| GroupNotifyGuildMemberRequest
| GroupNotifyGuildMemberRemoved
| GroupNotifyGuildSetAdmin
export interface GroupNotifyBase {
seq: string // '1716209619000000'
type: number // 7 申请入群11 已退群
status: number // 0 无需操作1 未操作2 已操作
group: {
groupCode: string
groupName: string
}
user1: {
uid: string
nickName: string
}
user2: {
uid: string
nickName: string
}
actionUser: {
uid: string
nickName: string
}
actionTime: string // '0'
actionTime: string // '0'status 变 2/3 以后有值
invitationExt: {
srcType: number // 0
groupCode: string // '0'
waitStatus: number // 0
/**
* 2
*/
waitStatus: 0 | 2
}
/**
@ -256,7 +247,155 @@ export interface GroupNotify {
*/
postscript: string
repeatSeqs: []
repeatSeqs: string[]
warningTips: string // '该账号存在风险,请谨慎操作'
}
export interface GroupNotifyGuildRequest extends GroupNotifyBase {
/**
* guild-request
*/
type: 1
/**
* 1
* 2
* 3
*/
status: 1 | 2 | 3
/**
*
*/
user1: {
uid: string
nickName: string
}
/**
*
*/
user2: {
uid: string
nickName: string
}
/**
*
*/
actionUser: {
uid: string
nickName: string
}
}
export interface GroupNotifyGuildMemberRequest extends GroupNotifyBase {
/**
* guild-member-request
*/
type: 7
/**
* 1
* 2
* 3
*/
status: 1 | 2 | 3
/**
*
*/
user1: {
uid: string
nickName: string
}
/**
*
*/
user2: {
uid: ''
nickName: ''
}
/**
*
*/
actionUser: {
uid: string
nickName: string
}
}
export interface GroupNotifyGuildMemberRemoved extends GroupNotifyBase {
/**
* guild-member-removed
*/
type: 11
/**
*
*/
status: 0
/**
* 退
*/
user1: {
uid: string
nickName: string
}
/**
*
*/
user2: {
uid: ''
nickName: ''
}
/**
*
*/
actionUser: {
uid: ''
nickName: ''
}
}
export interface GroupNotifyGuildSetAdmin extends GroupNotifyBase {
/**
*
*/
type: 8
/**
*
*/
status: 0
/**
*
*/
user1: {
uid: string
nickName: string
}
/**
*
*/
user2: {
uid: string
nickName: string
}
/**
*
*/
actionUser: {
uid: ''
nickName: ''
}
}