Skip to content

Commit

Permalink
fix(core): fix parent permission registration, close #1200
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 17, 2023
1 parent 6fbe0d0 commit e448f00
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
15 changes: 8 additions & 7 deletions packages/core/src/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ export class Commander extends Map<string, Command> {

ctx.before('attach', (session) => {
// strip prefix
let content = session.parsed.content
let content = session.stripped.content
for (const prefix of this._resolvePrefixes(session)) {
if (!content.startsWith(prefix)) continue
session.parsed.prefix = prefix
session.stripped.prefix = prefix
content = content.slice(prefix.length)
break
}
Expand All @@ -95,9 +95,9 @@ export class Commander extends Map<string, Command> {
ctx.middleware((session, next) => {
// use `!prefix` instead of `prefix === null` to prevent from blocking other middlewares
// we need to make sure that the user truly has the intension to call a command
const { argv, quote, isDirect, parsed: { prefix, appel } } = session
const { argv, quote, isDirect, stripped: { prefix, appel } } = session
if (argv.command || !isDirect && !prefix && !appel) return next()
const content = session.parsed.content.slice((prefix ?? '').length)
const content = session.stripped.content.slice((prefix ?? '').length)
const actual = content.split(/\s/, 1)[0].toLowerCase()
if (!actual) return next()

Expand Down Expand Up @@ -203,11 +203,13 @@ export class Commander extends Map<string, Command> {
}
return parent = command
}
command = new Command(name, index === segments.length - 1 ? decl : '', caller)
const isLast = index === segments.length - 1
command = new Command(name, isLast ? decl : '', caller)
command._disposables.push(caller.i18n.define('', {
[`commands.${command.name}.$`]: '',
[`commands.${command.name}.description`]: index === segments.length - 1 ? desc : '',
[`commands.${command.name}.description`]: isLast ? desc : '',
}))
command._disposables.push(caller.permissions.config(`command.${name}`, isLast ? config : {}, 1))
created.push(command)
root ||= command
if (parent) {
Expand All @@ -219,7 +221,6 @@ export class Commander extends Map<string, Command> {
Object.assign(parent.config, config)
created.forEach(command => caller.emit('command-added', command))
parent[Context.current] = caller
parent._disposables.push(this.caller.permissions.config(`command.${parent.name}`, parent.config, 1))
if (root) caller.collect(`command <${root.name}>`, () => root.dispose())
return parent
}
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ export class Processor {
if (!i18n) {
match(pattern)
} else {
for (const locale in this.ctx.i18n._data) {
for (const locale of this.ctx.i18n.fallback([])) {
const store = this.ctx.i18n._data[locale]
let value = store[pattern as string] as string | RegExp
let value = store?.[pattern as string] as string | RegExp
if (!value) continue
if (regex) {
const rest = fuzzy ? `(?:${stripped.appel ? '' : '\\s+'}([\\s\\S]*))?` : ''
Expand Down Expand Up @@ -298,21 +298,21 @@ export namespace SharedCache {
export interface Entry<T> {
value: T
key: string
refs: Set<string>
refs: Set<number>
}
}

export class SharedCache<T> {
#keyMap = new Map<string, SharedCache.Entry<T>>()

get(ref: string, key: string) {
get(ref: number, key: string) {
const entry = this.#keyMap.get(key)
if (!entry) return
entry.refs.add(ref)
return entry.value
}

set(ref: string, key: string, value: T) {
set(ref: number, key: string, value: T) {
let entry = this.#keyMap.get(key)
if (entry) {
entry.value = value
Expand All @@ -323,7 +323,7 @@ export class SharedCache<T> {
entry.refs.add(ref)
}

delete(ref: string) {
delete(ref: number) {
for (const key of [...this.#keyMap.keys()]) {
const { refs } = this.#keyMap.get(key)
refs.delete(ref)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,9 @@ extend(Session.prototype as Session.Private, {
if (argv.command) return argv.command
if (argv.name) return argv.command = this.app.$commander.resolve(argv.name)

const { parsed, isDirect } = this
const { stripped, isDirect } = this
// guild message should have prefix or appel to be interpreted as a command call
if (argv.root && !isDirect && parsed.prefix === null && !parsed.appel) return
if (argv.root && !isDirect && stripped.prefix === null && !stripped.appel) return
const segments: string[] = []
while (argv.tokens.length) {
const { content } = argv.tokens[0]
Expand Down

0 comments on commit e448f00

Please sign in to comment.