mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 06:47:26 +00:00
refactor(discord): share component DM auth context
This commit is contained in:
@@ -35,6 +35,10 @@ type DiscordUser = Parameters<typeof formatDiscordUserTag>[0];
|
|||||||
|
|
||||||
type AgentComponentInteraction = ButtonInteraction | StringSelectMenuInteraction;
|
type AgentComponentInteraction = ButtonInteraction | StringSelectMenuInteraction;
|
||||||
|
|
||||||
|
type ComponentInteractionContext = NonNullable<
|
||||||
|
Awaited<ReturnType<typeof resolveComponentInteractionContext>>
|
||||||
|
>;
|
||||||
|
|
||||||
type DiscordChannelContext = {
|
type DiscordChannelContext = {
|
||||||
channelName: string | undefined;
|
channelName: string | undefined;
|
||||||
channelSlug: string;
|
channelSlug: string;
|
||||||
@@ -349,6 +353,34 @@ async function ensureDmComponentAuthorized(params: {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function resolveInteractionContextWithDmAuth(params: {
|
||||||
|
ctx: AgentComponentContext;
|
||||||
|
interaction: AgentComponentInteraction;
|
||||||
|
label: string;
|
||||||
|
componentLabel: string;
|
||||||
|
}): Promise<ComponentInteractionContext | null> {
|
||||||
|
const interactionCtx = await resolveComponentInteractionContext({
|
||||||
|
interaction: params.interaction,
|
||||||
|
label: params.label,
|
||||||
|
});
|
||||||
|
if (!interactionCtx) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (interactionCtx.isDirectMessage) {
|
||||||
|
const authorized = await ensureDmComponentAuthorized({
|
||||||
|
ctx: params.ctx,
|
||||||
|
interaction: params.interaction,
|
||||||
|
user: interactionCtx.user,
|
||||||
|
componentLabel: params.componentLabel,
|
||||||
|
replyOpts: interactionCtx.replyOpts,
|
||||||
|
});
|
||||||
|
if (!authorized) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return interactionCtx;
|
||||||
|
}
|
||||||
|
|
||||||
export class AgentComponentButton extends Button {
|
export class AgentComponentButton extends Button {
|
||||||
label = AGENT_BUTTON_KEY;
|
label = AGENT_BUTTON_KEY;
|
||||||
customId = `${AGENT_BUTTON_KEY}:seed=1`;
|
customId = `${AGENT_BUTTON_KEY}:seed=1`;
|
||||||
@@ -378,9 +410,11 @@ export class AgentComponentButton extends Button {
|
|||||||
|
|
||||||
const { componentId } = parsed;
|
const { componentId } = parsed;
|
||||||
|
|
||||||
const interactionCtx = await resolveComponentInteractionContext({
|
const interactionCtx = await resolveInteractionContextWithDmAuth({
|
||||||
|
ctx: this.ctx,
|
||||||
interaction,
|
interaction,
|
||||||
label: "agent button",
|
label: "agent button",
|
||||||
|
componentLabel: "button",
|
||||||
});
|
});
|
||||||
if (!interactionCtx) {
|
if (!interactionCtx) {
|
||||||
return;
|
return;
|
||||||
@@ -396,19 +430,6 @@ export class AgentComponentButton extends Button {
|
|||||||
memberRoleIds,
|
memberRoleIds,
|
||||||
} = interactionCtx;
|
} = interactionCtx;
|
||||||
|
|
||||||
if (isDirectMessage) {
|
|
||||||
const authorized = await ensureDmComponentAuthorized({
|
|
||||||
ctx: this.ctx,
|
|
||||||
interaction,
|
|
||||||
user,
|
|
||||||
componentLabel: "button",
|
|
||||||
replyOpts,
|
|
||||||
});
|
|
||||||
if (!authorized) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// P2 FIX: Check user allowlist before processing component interaction
|
// P2 FIX: Check user allowlist before processing component interaction
|
||||||
// This prevents unauthorized users from injecting system events
|
// This prevents unauthorized users from injecting system events
|
||||||
const guildInfo = resolveDiscordGuildEntry({
|
const guildInfo = resolveDiscordGuildEntry({
|
||||||
@@ -496,9 +517,11 @@ export class AgentSelectMenu extends StringSelectMenu {
|
|||||||
|
|
||||||
const { componentId } = parsed;
|
const { componentId } = parsed;
|
||||||
|
|
||||||
const interactionCtx = await resolveComponentInteractionContext({
|
const interactionCtx = await resolveInteractionContextWithDmAuth({
|
||||||
|
ctx: this.ctx,
|
||||||
interaction,
|
interaction,
|
||||||
label: "agent select",
|
label: "agent select",
|
||||||
|
componentLabel: "select menu",
|
||||||
});
|
});
|
||||||
if (!interactionCtx) {
|
if (!interactionCtx) {
|
||||||
return;
|
return;
|
||||||
@@ -514,19 +537,6 @@ export class AgentSelectMenu extends StringSelectMenu {
|
|||||||
memberRoleIds,
|
memberRoleIds,
|
||||||
} = interactionCtx;
|
} = interactionCtx;
|
||||||
|
|
||||||
if (isDirectMessage) {
|
|
||||||
const authorized = await ensureDmComponentAuthorized({
|
|
||||||
ctx: this.ctx,
|
|
||||||
interaction,
|
|
||||||
user,
|
|
||||||
componentLabel: "select menu",
|
|
||||||
replyOpts,
|
|
||||||
});
|
|
||||||
if (!authorized) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check user allowlist before processing component interaction
|
// Check user allowlist before processing component interaction
|
||||||
const guildInfo = resolveDiscordGuildEntry({
|
const guildInfo = resolveDiscordGuildEntry({
|
||||||
guild: interaction.guild ?? undefined,
|
guild: interaction.guild ?? undefined,
|
||||||
|
|||||||
Reference in New Issue
Block a user