Skip to content

Commit

Permalink
fix(koishi): command resolution respects session, fix #1483
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 12, 2025
1 parent 14e9c55 commit 9878506
Showing 2 changed files with 16 additions and 14 deletions.
17 changes: 9 additions & 8 deletions packages/core/src/command/index.ts
Original file line number Diff line number Diff line change
@@ -122,8 +122,9 @@ export class Commander {
expect: this.available(session),
suffix: session.text('internal.suggest-command'),
filter: (name) => {
name = this.resolve(name)!.name
return ctx.permissions.test(`command:${name}`, session, cache)
const command = this.resolve(name, session)
if (!command) return false
return ctx.permissions.test(`command:${command.name}`, session, cache)
},
})
if (!name) return next()
@@ -263,15 +264,15 @@ export class Commander {
.flatMap(cmd => Object.keys(cmd._aliases))
}

resolve(key: string) {
return this._resolve(key).command
resolve(key: string, session?: Session) {
return this._resolve(key, session).command
}

_resolve(key: string) {
_resolve(key: string, session?: Session) {
if (!key) return {}
const segments = Command.normalize(key).split('.')
let i = 1, name = segments[0], command: Command
while ((command = this.get(name)) && i < segments.length) {
while ((command = this.get(name, session)) && i < segments.length) {
name = command.name + '.' + segments[i++]
}
return { command, name }
@@ -280,7 +281,7 @@ export class Commander {
inferCommand(argv: Argv) {
if (!argv) return
if (argv.command) return argv.command
if (argv.name) return argv.command = this.resolve(argv.name)
if (argv.name) return argv.command = this.resolve(argv.name, argv.session)

const { stripped, isDirect, quote } = argv.session
// guild message should have prefix or appel to be interpreted as a command call
@@ -290,7 +291,7 @@ export class Commander {
while (argv.tokens.length) {
const { content } = argv.tokens[0]
segments.push(content)
const { name, command } = this._resolve(segments.join('.'))
const { name, command } = this._resolve(segments.join('.'), argv.session)
if (!command) break
argv.tokens.shift()
argv.command = command
13 changes: 7 additions & 6 deletions plugins/common/help/src/index.ts
Original file line number Diff line number Diff line change
@@ -95,13 +95,13 @@ export function apply(ctx: Context, config: Config) {

const $ = ctx.$commander
function findCommand(target: string, session: Session<never, never>) {
const command = $.resolve(target)
const command = $.resolve(target, session)
if (command?.ctx.filter(session)) return command

// shortcuts
const data = ctx.i18n
.find('commands.(name).shortcuts.(variant)', target)
.map(item => ({ ...item, command: $.resolve(item.data.name) }))
.map(item => ({ ...item, command: $.resolve(item.data.name, session) }))
.filter(item => item.command?.match(session))
const perfect = data.filter(item => item.similarity === 1)
if (!perfect.length) return data
@@ -137,11 +137,12 @@ export function apply(ctx: Context, config: Config) {
prefix: session.text('.not-found'),
suffix: session.text('internal.suggest-command'),
filter: (name) => {
name = $.resolve(name)!.name
return ctx.permissions.test(`command:${name}`, session, cache)
const command = $.resolve(name, session)
if (!command) return false
return ctx.permissions.test(`command:${command.name}`, session, cache)
},
})
return $.resolve(name)
return $.resolve(name, session)
}

const cmd = ctx.command('help [command:string]', { authority: 0, ...config })
@@ -173,7 +174,7 @@ export function apply(ctx: Context, config: Config) {
function* getCommands(session: Session<'authority'>, commands: Command[], showHidden = false): Generator<Command> {
for (const command of commands) {
if (!showHidden && session.resolve(command.config.hidden)) continue
if (command.match(session)) {
if (command.match(session) && Object.keys(command._aliases).length) {
yield command
} else {
yield* getCommands(session, command.children, showHidden)

0 comments on commit 9878506

Please sign in to comment.