mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
refactor
This commit is contained in:
parent
44bfc5aab9
commit
273d4133eb
@ -2,24 +2,22 @@ import fs from 'node:fs'
|
||||
import BaseAction from '../BaseAction'
|
||||
import { ActionName } from '../types'
|
||||
import { SendElementEntities } from '@/ntqqapi/entities'
|
||||
import { ChatType, SendFileElement } from '@/ntqqapi/types'
|
||||
import { SendFileElement } from '@/ntqqapi/types'
|
||||
import { uri2local } from '@/common/utils'
|
||||
import { Peer } from '@/ntqqapi/types'
|
||||
import { sendMsg } from '../../helper/createMessage'
|
||||
import { sendMsg, createPeer, CreatePeerMode } from '../../helper/createMessage'
|
||||
|
||||
interface Payload {
|
||||
user_id: number | string
|
||||
group_id?: number | string
|
||||
interface UploadGroupFilePayload {
|
||||
group_id: number | string
|
||||
file: string
|
||||
name: string
|
||||
folder?: string
|
||||
folder_id?: string
|
||||
}
|
||||
|
||||
export class UploadGroupFile extends BaseAction<Payload, null> {
|
||||
export class UploadGroupFile extends BaseAction<UploadGroupFilePayload, null> {
|
||||
actionName = ActionName.GoCQHTTP_UploadGroupFile
|
||||
|
||||
protected async _handle(payload: Payload): Promise<null> {
|
||||
protected async _handle(payload: UploadGroupFilePayload): Promise<null> {
|
||||
let file = payload.file
|
||||
if (fs.existsSync(file)) {
|
||||
file = `file://${file}`
|
||||
@ -29,31 +27,23 @@ export class UploadGroupFile extends BaseAction<Payload, null> {
|
||||
throw new Error(downloadResult.errMsg)
|
||||
}
|
||||
const sendFileEle = await SendElementEntities.file(this.ctx, downloadResult.path, payload.name, payload.folder_id)
|
||||
await sendMsg(this.ctx, {
|
||||
chatType: ChatType.group,
|
||||
peerUid: payload.group_id?.toString()!,
|
||||
}, [sendFileEle], [], true)
|
||||
const peer = await createPeer(this.ctx, payload, CreatePeerMode.Group)
|
||||
await sendMsg(this.ctx, peer, [sendFileEle], [], true)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export class UploadPrivateFile extends BaseAction<Payload, null> {
|
||||
interface UploadPrivateFilePayload {
|
||||
user_id: number | string
|
||||
file: string
|
||||
name: string
|
||||
}
|
||||
|
||||
export class UploadPrivateFile extends BaseAction<UploadPrivateFilePayload, null> {
|
||||
actionName = ActionName.GoCQHTTP_UploadPrivateFile
|
||||
|
||||
async getPeer(payload: Payload): Promise<Peer> {
|
||||
if (payload.user_id) {
|
||||
const peerUid = await this.ctx.ntUserApi.getUidByUin(payload.user_id.toString())
|
||||
if (!peerUid) {
|
||||
throw `私聊${payload.user_id}不存在`
|
||||
}
|
||||
const isBuddy = await this.ctx.ntFriendApi.isBuddy(peerUid)
|
||||
return { chatType: isBuddy ? ChatType.friend : ChatType.temp, peerUid }
|
||||
}
|
||||
throw '缺少参数 user_id'
|
||||
}
|
||||
|
||||
protected async _handle(payload: Payload): Promise<null> {
|
||||
const peer = await this.getPeer(payload)
|
||||
protected async _handle(payload: UploadPrivateFilePayload): Promise<null> {
|
||||
const peer = await createPeer(this.ctx, payload, CreatePeerMode.Private)
|
||||
let file = payload.file
|
||||
if (fs.existsSync(file)) {
|
||||
file = `file://${file}`
|
||||
|
@ -21,45 +21,15 @@ import { CustomMusicSignPostData, IdMusicSignPostData, MusicSign, MusicSignPostD
|
||||
import { Peer } from '@/ntqqapi/types/msg'
|
||||
import { MessageUnique } from '@/common/utils/messageUnique'
|
||||
import { selfInfo } from '@/common/globalVars'
|
||||
import { convertMessage2List, createSendElements, sendMsg } from '../../helper/createMessage'
|
||||
import { convertMessage2List, createSendElements, sendMsg, createPeer, CreatePeerMode } from '../../helper/createMessage'
|
||||
|
||||
export interface ReturnDataType {
|
||||
interface ReturnData {
|
||||
message_id: number
|
||||
}
|
||||
|
||||
export enum ContextMode {
|
||||
Normal = 0,
|
||||
Private = 1,
|
||||
Group = 2
|
||||
}
|
||||
|
||||
export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
||||
export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnData> {
|
||||
actionName = ActionName.SendMsg
|
||||
|
||||
private async createContext(payload: OB11PostSendMsg, contextMode: ContextMode): Promise<Peer> {
|
||||
// This function determines the type of message by the existence of user_id / group_id,
|
||||
// not message_type.
|
||||
// This redundant design of Ob11 here should be blamed.
|
||||
|
||||
if ((contextMode === ContextMode.Group || contextMode === ContextMode.Normal) && payload.group_id) {
|
||||
return {
|
||||
chatType: ChatType.group,
|
||||
peerUid: payload.group_id.toString(),
|
||||
}
|
||||
}
|
||||
if ((contextMode === ContextMode.Private || contextMode === ContextMode.Normal) && payload.user_id) {
|
||||
const uid = await this.ctx.ntUserApi.getUidByUin(payload.user_id.toString())
|
||||
if (!uid) throw new Error('无法获取用户信息')
|
||||
const isBuddy = await this.ctx.ntFriendApi.isBuddy(uid)
|
||||
return {
|
||||
chatType: isBuddy ? ChatType.friend : ChatType.temp,
|
||||
peerUid: uid,
|
||||
guildId: isBuddy ? '' : payload.group_id?.toString() || ''
|
||||
}
|
||||
}
|
||||
throw new Error('请指定 group_id 或 user_id')
|
||||
}
|
||||
|
||||
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
|
||||
const messages = convertMessage2List(payload.message)
|
||||
const fmNum = this.getSpecialMsgNum(messages, OB11MessageDataType.node)
|
||||
@ -84,13 +54,13 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
||||
}
|
||||
|
||||
protected async _handle(payload: OB11PostSendMsg) {
|
||||
let contextMode = ContextMode.Normal
|
||||
let contextMode = CreatePeerMode.Normal
|
||||
if (payload.message_type === 'group') {
|
||||
contextMode = ContextMode.Group
|
||||
contextMode = CreatePeerMode.Group
|
||||
} else if (payload.message_type === 'private') {
|
||||
contextMode = ContextMode.Private
|
||||
contextMode = CreatePeerMode.Private
|
||||
}
|
||||
const peer = await this.createContext(payload, contextMode)
|
||||
const peer = await createPeer(this.ctx, payload, contextMode)
|
||||
const messages = convertMessage2List(
|
||||
payload.message,
|
||||
payload.auto_escape === true || payload.auto_escape === 'true',
|
||||
|
@ -276,4 +276,35 @@ export async function sendMsg(
|
||||
deleteAfterSentFiles.map(path => fsPromise.unlink(path))
|
||||
return returnMsg
|
||||
}
|
||||
}
|
||||
|
||||
export interface CreatePeerPayload {
|
||||
group_id?: string | number
|
||||
user_id?: string | number
|
||||
}
|
||||
|
||||
export enum CreatePeerMode {
|
||||
Normal = 0,
|
||||
Private = 1,
|
||||
Group = 2
|
||||
}
|
||||
|
||||
export async function createPeer(ctx: Context, payload: CreatePeerPayload, mode: CreatePeerMode): Promise<Peer> {
|
||||
if ((mode === CreatePeerMode.Group || mode === CreatePeerMode.Normal) && payload.group_id) {
|
||||
return {
|
||||
chatType: ChatType.group,
|
||||
peerUid: payload.group_id.toString(),
|
||||
}
|
||||
}
|
||||
if ((mode === CreatePeerMode.Private || mode === CreatePeerMode.Normal) && payload.user_id) {
|
||||
const uid = await ctx.ntUserApi.getUidByUin(payload.user_id.toString())
|
||||
if (!uid) throw new Error('无法获取用户信息')
|
||||
const isBuddy = await ctx.ntFriendApi.isBuddy(uid)
|
||||
return {
|
||||
chatType: isBuddy ? ChatType.friend : ChatType.temp,
|
||||
peerUid: uid,
|
||||
guildId: isBuddy ? '' : payload.group_id?.toString() || ''
|
||||
}
|
||||
}
|
||||
throw new Error('请指定 group_id 或 user_id')
|
||||
}
|
@ -1,12 +1,12 @@
|
||||
import { OB11Message, OB11MessageAt, OB11MessageData, OB11MessageDataType } from '../types'
|
||||
import { OB11FriendRequestEvent } from '../event/request/OB11FriendRequest'
|
||||
import { OB11GroupRequestEvent } from '../event/request/OB11GroupRequest'
|
||||
import { ChatType, GroupRequestOperateTypes, Peer } from '@/ntqqapi/types'
|
||||
import { convertMessage2List, createSendElements, sendMsg } from '../helper/createMessage'
|
||||
import { getConfigUtil } from '@/common/config'
|
||||
import { GroupRequestOperateTypes } from '@/ntqqapi/types'
|
||||
import { convertMessage2List, createSendElements, sendMsg, createPeer, CreatePeerMode } from '../helper/createMessage'
|
||||
import { MessageUnique } from '@/common/utils/messageUnique'
|
||||
import { isNullable } from 'cosmokit'
|
||||
import { Context } from 'cordis'
|
||||
import { OB11Config } from '@/common/types'
|
||||
|
||||
interface QuickOperationPrivateMessage {
|
||||
reply?: string
|
||||
@ -57,21 +57,14 @@ export async function handleQuickOperation(ctx: Context, event: QuickOperationEv
|
||||
|
||||
async function handleMsg(ctx: Context, msg: OB11Message, quickAction: QuickOperationPrivateMessage | QuickOperationGroupMessage) {
|
||||
const reply = quickAction.reply
|
||||
const ob11Config = getConfigUtil().getConfig().ob11
|
||||
const peer: Peer = {
|
||||
chatType: ChatType.friend,
|
||||
peerUid: msg.user_id.toString(),
|
||||
}
|
||||
if (msg.message_type == 'private') {
|
||||
peer.peerUid = (await ctx.ntUserApi.getUidByUin(msg.user_id.toString()))!
|
||||
if (msg.sub_type === 'group') {
|
||||
peer.chatType = ChatType.temp
|
||||
}
|
||||
}
|
||||
else {
|
||||
peer.chatType = ChatType.group
|
||||
peer.peerUid = msg.group_id?.toString()!
|
||||
const ob11Config: OB11Config = ctx.config
|
||||
let contextMode = CreatePeerMode.Normal
|
||||
if (msg.message_type === 'group') {
|
||||
contextMode = CreatePeerMode.Group
|
||||
} else if (msg.message_type === 'private') {
|
||||
contextMode = CreatePeerMode.Private
|
||||
}
|
||||
const peer = await createPeer(ctx, msg, contextMode)
|
||||
if (reply) {
|
||||
let replyMessage: OB11MessageData[] = []
|
||||
if (ob11Config.enableQOAutoQuote) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user