Skip to content

Commit

Permalink
feat(core): symmetric command api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 18, 2020
1 parent a16c113 commit cdb98c0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,14 @@ export class App extends Context {
private _parse(message: string, session: Session, builtin: boolean, terminator = '') {
// group message should have prefix or appel to be interpreted as a command call
const { $reply, $prefix, $appel, messageType } = session
if (builtin && ($reply || messageType !== 'private' && $prefix === null && !$appel)) return
if (builtin && messageType !== 'private' && $prefix === null && !$appel) return
terminator = escapeRegExp(terminator)
const name = message.split(new RegExp(`[\\s${terminator}]`), 1)[0]
const index = name.lastIndexOf('/')
const command = this.app._commandMap[name.slice(index + 1).toLowerCase()]
if (!command) return
const result = command.parse(message.slice(name.length).trimStart(), terminator)
message = message.slice(name.length).trim() + ($reply ? ' ' + $reply.message : '')
const result = command.parse(message, terminator)
return { command, ...result }
}
}
4 changes: 2 additions & 2 deletions packages/koishi-core/src/plugins/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Session.prototype.$suggest = function $suggest(this: Session, options: SuggestOp
export default function apply(ctx: Context) {
ctx.middleware((session, next) => {
const { $argv, $reply, $parsed, $prefix, $appel, messageType } = session
if ($argv || $reply || messageType !== 'private' && $prefix === null && !$appel) return next()
if ($argv || messageType !== 'private' && $prefix === null && !$appel) return next()
const target = $parsed.split(/\s/, 1)[0].toLowerCase()
if (!target) return next()

Expand All @@ -103,7 +103,7 @@ export default function apply(ctx: Context) {
prefix: Message.COMMAND_SUGGEST_PREFIX,
suffix: Message.COMMAND_SUGGEST_SUFFIX,
async apply(suggestion, next) {
const newMessage = suggestion + $parsed.slice(target.length)
const newMessage = suggestion + $parsed.slice(target.length) + ($reply ? ' ' + $reply.message : '')
return this.$execute(newMessage, next)
},
})
Expand Down

0 comments on commit cdb98c0

Please sign in to comment.