Skip to content

Commit

Permalink
feat: support experimental meta.$stripped property
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 19, 2020
1 parent 3f0e69c commit e5cfad6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,18 @@ export class App extends Context {
private _preprocess = async (meta: MessageMeta, next: NextFunction) => {
// strip prefix
let capture: RegExpMatchArray
let atMe = false, nickname = false, prefix: string = null
let atMe = false, nickname = '', prefix: string = null
let message = simplify(meta.message.trim())
let parsedArgv: ParsedCommandLine

if (meta.messageType !== 'private' && (capture = message.match(this.atMeRE))) {
atMe = true
nickname = true
nickname = capture[0]
message = message.slice(capture[0].length)
}

if ((capture = message.match(this.nicknameRE))?.[0].length) {
nickname = true
nickname = capture[0]
message = message.slice(capture[0].length)
}

Expand All @@ -283,6 +283,9 @@ export class App extends Context {
message = message.slice(capture[0].length)
}

// store parsed message
meta.$stripped = { atMe, nickname, prefix, message }

// parse as command
if (prefix !== null || nickname || meta.messageType === 'private') {
parsedArgv = this.parseCommandLine(message, meta)
Expand Down
10 changes: 10 additions & 0 deletions packages/koishi-core/src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ export interface ResponsePayload {
reason?: string
}

export interface StrippedMessage {
atMe?: boolean
nickname?: string
prefix?: string
message?: string
}

/** CQHTTP Meta Information */
export interface Meta <T extends PostType = PostType> {
// database bindings
Expand All @@ -52,6 +59,9 @@ export interface Meta <T extends PostType = PostType> {
$ctxId?: number
$ctxType?: ContextType

// other properties
$stripped?: StrippedMessage

// quick operations
$response?: (payload: ResponsePayload) => void
$delete?: () => Promise<void>
Expand Down

0 comments on commit e5cfad6

Please sign in to comment.