From 57cded0f481bde3f510f478d9d52bb983339d6e9 Mon Sep 17 00:00:00 2001 From: Shigma Date: Tue, 8 Aug 2023 15:41:56 +0800 Subject: [PATCH] fix(core): fix session.permissions maybe undefined --- packages/core/src/command/validate.ts | 2 +- packages/core/src/middleware.ts | 3 --- packages/core/src/permission.ts | 13 +++++++++---- packages/core/src/session.ts | 1 + 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/core/src/command/validate.ts b/packages/core/src/command/validate.ts index 130924a6b0..9177e0369a 100644 --- a/packages/core/src/command/validate.ts +++ b/packages/core/src/command/validate.ts @@ -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) diff --git a/packages/core/src/middleware.ts b/packages/core/src/middleware.ts index a84e2d16af..493a75158b 100644 --- a/packages/core/src/middleware.ts +++ b/packages/core/src/middleware.ts @@ -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 @@ -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 @@ -280,7 +278,6 @@ export class Processor { const userFields = new Set(['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 diff --git a/packages/core/src/permission.ts b/packages/core/src/permission.ts index 130235f529..8f1268befa 100644 --- a/packages/core/src/permission.ts +++ b/packages/core/src/permission.ts @@ -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 { @@ -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 @@ -140,11 +146,10 @@ export class Permissions { return [...this.#inherits.store.keys()] } - async test(x: string[], y: Iterable, session: Partial = {}) { + async test(y: Iterable, session: Partial = {}) { const cache: Dict> = 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 diff --git a/packages/core/src/session.ts b/packages/core/src/session.ts index c14b0930db..09612b20a2 100644 --- a/packages/core/src/session.ts +++ b/packages/core/src/session.ts @@ -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) },