Skip to content

Commit

Permalink
fix(core): fix session.permissions maybe undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 8, 2023
1 parent 1b8fe23 commit 57cded0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/command/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function validate(ctx: Context) {
permissions.push(`command.${command.name}.option.${option.name}`)
}
}
if (!await ctx.permissions.test(session.permissions, permissions, session as any)) {
if (!await ctx.permissions.test(permissions, session as any)) {
return sendHint('internal.low-authority')
}
}, true)
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ export class Processor {
defineProperty(session, 'parsed', { hasMention, content, appel, prefix: null })
this.ctx.emit(session, 'before-attach', session)

session.permissions = []
if (this.ctx.database) {
if (!session.isDirect) {
// attach group data
Expand All @@ -265,7 +264,6 @@ export class Processor {
const channel = await session.observeChannel(channelFields)
// for backwards compatibility
channel.guildId = session.guildId
session.permissions.push(...channel.permissions)

// emit attach event
if (await this.ctx.serial(session, 'attach-channel', session)) return
Expand All @@ -280,7 +278,6 @@ export class Processor {
const userFields = new Set<User.Field>(['id', 'flag', 'authority', 'permissions', 'locales'])
this.ctx.emit('before-attach-user', session, userFields)
const user = await session.observeUser(userFields)
session.permissions.push(`user.${user.id}`, ...user.permissions)

// emit attach event
if (await this.ctx.serial(session, 'attach-user', session)) return
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export class Permissions {
this.provide('bot.*', async (name, session) => {
return session.bot?.supports(name.slice(4), session)
})

this.provide('*', async (name, session) => {
return session.permissions?.includes(name)
|| session.user?.permissions?.includes(name)
|| session.channel?.permissions?.includes(name)
})
}

private get caller(): Context {
Expand All @@ -96,9 +102,9 @@ export class Permissions {
.map(([key, value]) => value)
if (!callbacks.length) return false
for (const callback of callbacks) {
if (!await callback(name, session)) return false
if (await callback(name, session)) return true
}
return true
return false
} catch (error) {
logger.warn(error)
return false
Expand Down Expand Up @@ -140,11 +146,10 @@ export class Permissions {
return [...this.#inherits.store.keys()]
}

async test(x: string[], y: Iterable<string>, session: Partial<Session> = {}) {
async test(y: Iterable<string>, session: Partial<Session> = {}) {
const cache: Dict<Promise<boolean>> = Object.create(null)
for (const name of this.#depends.subgraph(y, session)) {
const parents = [...this.#inherits.subgraph([name], session)]
if (parents.some(parent => x.includes(parent))) continue
const results = await Promise.all(parents.map(parent => cache[parent] ||= this.check(parent, session)))
if (results.some(result => result)) continue
return false
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ extend(Session.prototype as Session.Private, {
defineProperty(this, 'user', null)
defineProperty(this, 'channel', null)
defineProperty(this, 'guild', null)
defineProperty(this, 'permissions', [])
defineProperty(this, '_queuedTasks', [])
defineProperty(this, '_queuedTimeout', null)
},
Expand Down

0 comments on commit 57cded0

Please sign in to comment.