fix(discord): normalize command allowFrom prefixes

This commit is contained in:
Sebastian
2026-02-17 08:45:12 -05:00
parent 96fb276481
commit dff8692613
3 changed files with 68 additions and 11 deletions

View File

@@ -253,6 +253,15 @@ describe("resolveCommandAuthorization", () => {
} as MsgContext;
}
function makeDiscordContext(senderId: string, fromOverride?: string): MsgContext {
return {
Provider: "discord",
Surface: "discord",
From: fromOverride ?? `discord:${senderId}`,
SenderId: senderId,
} as MsgContext;
}
function resolveWithCommandsAllowFrom(senderId: string, commandAuthorized: boolean) {
return resolveCommandAuthorization({
ctx: makeWhatsAppContext(senderId),
@@ -372,6 +381,48 @@ describe("resolveCommandAuthorization", () => {
expect(auth.isAuthorizedSender).toBe(true);
});
it("normalizes Discord commands.allowFrom prefixes and mentions", () => {
const cfg = {
commands: {
allowFrom: {
discord: ["user:123", "<@!456>", "pk:member-1"],
},
},
} as OpenClawConfig;
const userAuth = resolveCommandAuthorization({
ctx: makeDiscordContext("123"),
cfg,
commandAuthorized: false,
});
expect(userAuth.isAuthorizedSender).toBe(true);
const mentionAuth = resolveCommandAuthorization({
ctx: makeDiscordContext("456"),
cfg,
commandAuthorized: false,
});
expect(mentionAuth.isAuthorizedSender).toBe(true);
const pkAuth = resolveCommandAuthorization({
ctx: makeDiscordContext("member-1", "discord:999"),
cfg,
commandAuthorized: false,
});
expect(pkAuth.isAuthorizedSender).toBe(true);
const deniedAuth = resolveCommandAuthorization({
ctx: makeDiscordContext("other"),
cfg,
commandAuthorized: false,
});
expect(deniedAuth.isAuthorizedSender).toBe(false);
});
});
});