Skip to content

Commit

Permalink
feat(core): object form session.$reply
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 18, 2020
1 parent abe85f8 commit a16c113
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
8 changes: 6 additions & 2 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { simplify, defineProperty, Time, Observed, coerce, escapeRegExp, makeArray, noop } from 'koishi-utils'
import { Command } from './command'
import { Context, Middleware, NextFunction } from './context'
import { Group, User, Database } from './database'
import { BotOptions, Server } from './server'
import { Session } from './session'
import { simplify, defineProperty, Time, Observed, coerce, escapeRegExp, makeArray } from 'koishi-utils'
import help from './plugins/help'
import shortcut from './plugins/shortcut'
import suggest from './plugins/suggest'
Expand Down Expand Up @@ -145,8 +145,12 @@ export class App extends Context {
let capture: RegExpMatchArray, atSelf = false
// eslint-disable-next-line no-cond-assign
if (capture = message.match(/^\[CQ:reply,id=(-?\d+)\]\s*/)) {
session.$reply = +capture[1]
session.$reply = await session.$bot.getMsg(+capture[1]).catch(noop)
message = message.slice(capture[0].length)
if (session.$reply) {
const prefix = `[CQ:at,qq=${session.$reply.sender.userId}]`
message = message.slice(prefix.length).trimStart()
}
}

// strip prefix
Expand Down
11 changes: 10 additions & 1 deletion packages/koishi-core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class Session<U extends User.Field = never, G extends Group.Field = never
$appel?: boolean
$prefix?: string = null
$parsed?: string
$reply?: number
$reply?: MessageInfo
$uuid = Random.uuid()

private _delay?: number
Expand Down Expand Up @@ -316,6 +316,15 @@ export interface StatusInfo {
good: boolean
}

export interface MessageInfo {
time: number
messageType: MessageType
messageId: number
realId: number
sender: SenderInfo
message: string
}

/**
* get context unique id
* @example
Expand Down
5 changes: 3 additions & 2 deletions packages/plugin-common/src/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ export default function apply(ctx: Context, config: SenderConfig = {}) {

ctx.middleware((session, next) => {
const { $reply, $parsed } = session
const userId = interactions[$reply]
if (!$parsed || !userId) return next()
if (!$parsed || !$reply) return next()
const userId = interactions[$reply.messageId]
if (!userId) return next()
return session.$bot.sendPrivateMsg(userId, $parsed)
})

Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function apply(ctx: Context, config: Config = {}) {

ctx.on('before-attach-user', (session, fields) => {
if (!session.$reply) return
if (history[int32ToHex6(session.$reply)]) {
if (history[int32ToHex6(session.$reply.messageId)]) {
fields.add('ghAccessToken')
fields.add('ghRefreshToken')
}
Expand All @@ -147,7 +147,7 @@ export function apply(ctx: Context, config: Config = {}) {
ctx.middleware((session, next) => {
if (!session.$reply) return next()
const body = session.$parsed
const payloads = history[int32ToHex6(session.$reply)]
const payloads = history[int32ToHex6(session.$reply.messageId)]
if (!body || !payloads) return next()

let name: string, message: string
Expand Down

0 comments on commit a16c113

Please sign in to comment.