Skip to content

Commit b5d592a

Browse files
committed
fix: 角色问题
1 parent e277df3 commit b5d592a

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/controller/openai/platform/base.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const MODULE = 'src/openai/platform/base.ts'
2424

2525
export const defaultCtx = {
2626
apiKey: '', // 用户的 OpenAI apiKey
27-
role: CONST.ROLE.GUEST as Role,
27+
role: new Set(CONST.ROLE.GUEST) as Set<Role>,
2828
chatType: '单聊' as ChatType,
2929
conversationId: '',
3030
isRequestOpenAi: false, // 收到的消息是命令还是请求 OpenAI
@@ -467,17 +467,16 @@ export abstract class Base<T extends Platform> {
467467
if (subcommand) {
468468
const obj = this.commands[subcommand]
469469
const roles = obj.roles as Role[]
470-
if (!obj || !roles.includes(role)) {
470+
if (!obj || roles.every(i => !role.has(i))) {
471471
return genFail(`命令 ${subcommand} 不存在`)
472472
}
473473
cmdList.push({ name: subcommand, description: obj.description })
474474
} else {
475475
Object.entries(this.commands).forEach(([name, obj]) => {
476476
const roles = obj.roles as Role[]
477-
if (!roles.includes(role)) {
478-
return
477+
if (roles.some(i => role.has(i))) {
478+
cmdList.push({ name, description: obj.description })
479479
}
480-
cmdList.push({ name, description: obj.description })
481480
})
482481
}
483482

@@ -620,7 +619,7 @@ export abstract class Base<T extends Platform> {
620619
protected async handleCommandMessage(message: string) {
621620
for (const [command, commandObj] of Object.entries(this.commands)) {
622621
const roles = commandObj.roles as Role[]
623-
if (!roles.includes(this.ctx.role)) continue
622+
if (roles.every(i => !this.ctx.role.has(i))) continue
624623
if (message === command || message.startsWith(`${command} `)) {
625624
const params = message.slice(command.length).trim()
626625
this.logger.info(`${MODULE} 执行命令 ${command} 参数 ${params}`)
@@ -647,7 +646,7 @@ export abstract class Base<T extends Platform> {
647646
id: this.platform.id,
648647
reqId: this.request.reqId,
649648
userId: this.platform.ctx.userId,
650-
role: this.ctx.role,
649+
role: Array.from(this.ctx.role).join(','),
651650
chatType: this.ctx.chatType,
652651
})
653652
this.platform.logger = logger

src/controller/openai/platform/wechat.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class WeChatHandler extends Base<WeChat> {
9898
const { platform, appid, userId } = this.platform.ctx
9999

100100
if (WE_CHAT_CONFIG.WECHAT_ADMIN_USER_ID_LIST.includes(userId)) {
101-
this.ctx.role = CONST.ROLE.ADMIN
101+
this.ctx.role.add(CONST.ROLE.ADMIN)
102102
}
103103

104104
const apiKeyRes = await kv.getApiKey(platform, appid, userId)
@@ -120,9 +120,8 @@ export class WeChatHandler extends Base<WeChat> {
120120
await kv.setApiKey(platform, appid, userId, this.ctx.apiKey)
121121
}
122122

123-
if (this.ctx.role === CONST.ROLE.GUEST) {
124-
this.ctx.role = CONST.ROLE.USER
125-
}
123+
this.ctx.role.delete(CONST.ROLE.GUEST)
124+
this.ctx.role.add(CONST.ROLE.USER)
126125

127126
const chatTypeRes = await kv.getChatType(platform, appid, userId)
128127
if (!chatTypeRes.success) {

0 commit comments

Comments
 (0)