diff --git a/config/config.example.ts b/config/config.example.ts index bf6042b5..6c8b3d93 100644 --- a/config/config.example.ts +++ b/config/config.example.ts @@ -70,6 +70,9 @@ export default defineConfig("0.0.1", { maxOpenPerUser: 1, // Global staff roles that can see and manage tickets. staffRoleIds: ["111111111111111111"], + // Only staff can use /add, /mass_add and /remove. Defaults to true when omitted. + // Set to false to let anyone with access to the ticket manage its participants. + participantsStaffOnly: true, // Roles that are blocked from opening any ticket type by default. blockedRoleIds: ["222222222222222222"], // Roles mentioned in the welcome message when a ticket is opened. diff --git a/i18n/en/index.ts b/i18n/en/index.ts index 5d8f47a3..5dfd112f 100644 --- a/i18n/en/index.ts +++ b/i18n/en/index.ts @@ -119,7 +119,8 @@ const en: BaseTranslation = { records: { not_ticket_channel: "This interaction was not used in a ticket channel.", not_open_ticket: "This channel is not an open ticket.", - already_closed: "This ticket is already closed." + already_closed: "This ticket is already closed.", + only_staff_manage: "Only staff can manage this ticket's participants." }, panel: { no_visible_types: "You do not have access to any ticket types on this panel.", diff --git a/i18n/fr/index.ts b/i18n/fr/index.ts index f1f4ad51..132db220 100644 --- a/i18n/fr/index.ts +++ b/i18n/fr/index.ts @@ -118,7 +118,8 @@ const fr: Translation = { records: { not_ticket_channel: "Cette interaction n'a pas été utilisée dans un salon de ticket.", not_open_ticket: "Ce salon n'est pas un ticket ouvert.", - already_closed: "Ce ticket est déjà fermé." + already_closed: "Ce ticket est déjà fermé.", + only_staff_manage: "Seul le personnel peut gérer les participants de ce ticket." }, panel: { no_visible_types: "Vous n'avez accès à aucun type de ticket sur ce panneau.", diff --git a/i18n/i18n-types.ts b/i18n/i18n-types.ts index 4d62a6af..fe70238f 100644 --- a/i18n/i18n-types.ts +++ b/i18n/i18n-types.ts @@ -349,6 +349,10 @@ type RootTranslation = { * T​h​i​s​ ​t​i​c​k​e​t​ ​i​s​ ​a​l​r​e​a​d​y​ ​c​l​o​s​e​d​. */ already_closed: string; + /** + * Only staff can manage this ticket's participants. + */ + only_staff_manage: string; }; panel: { /** @@ -1413,6 +1417,10 @@ export type TranslationFunctions = { * This ticket is already closed. */ already_closed: () => LocalizedString; + /** + * Only staff can manage this ticket's participants. + */ + only_staff_manage: () => LocalizedString; }; panel: { /** diff --git a/src/config/index.ts b/src/config/index.ts index 9444beb2..9a3d45eb 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -54,6 +54,8 @@ interface ConfigV0_0_1 { channelNameTemplate: string; maxOpenPerUser: number; staffRoleIds: string[]; + /** Restrict /add, /mass_add and /remove to staff. Defaults to true when omitted. */ + participantsStaffOnly?: boolean; blockedRoleIds: string[]; mentionRoleIds: string[]; defaultWelcomeMessage?: string; diff --git a/src/features/commands/add/command.ts b/src/features/commands/add/command.ts index a2e3a0a6..cb51f12e 100644 --- a/src/features/commands/add/command.ts +++ b/src/features/commands/add/command.ts @@ -20,6 +20,7 @@ import { reply } from "@/core/respond"; import { getUserOption } from "@/features/commands/shared/options"; import { sendTicketLog } from "@/features/logs/service"; import { createTicketLogContext } from "@/features/logs/utils"; +import { hasTicketStaffAccess } from "@/features/tickets/config-access"; import { getInvitedUserIds, grantTicketParticipantAccess, @@ -27,7 +28,7 @@ import { updateInvitedUserIds } from "@/features/tickets/participants"; import { getOpenTicketByChannel } from "@/features/tickets/records"; -import { getInteractionUser } from "@/features/tickets/utils"; +import { getInteractionUser, getMemberRoleIds } from "@/features/tickets/utils"; export default defineCommand({ data: (LL) => ({ @@ -64,6 +65,17 @@ export default defineCommand({ return; } + if ( + (app.config.tickets.participantsStaffOnly ?? true) && + !hasTicketStaffAccess(app, openTicket.ticketType, getMemberRoleIds(interaction)) + ) { + await reply(app, interaction, { + content: LL.tickets.records.only_staff_manage(), + flags: MessageFlags.Ephemeral + }); + return; + } + const invitedUserIds = getInvitedUserIds(openTicket.ticket); if (selectedUser.userId === openTicket.ticket.createdBy) { diff --git a/src/features/commands/mass_add/command.ts b/src/features/commands/mass_add/command.ts index a48a25c3..580c1ec6 100644 --- a/src/features/commands/mass_add/command.ts +++ b/src/features/commands/mass_add/command.ts @@ -21,6 +21,7 @@ import type { BotApp } from "@/core/types"; import { getStringOption } from "@/features/commands/shared/options"; import { sendTicketLog } from "@/features/logs/service"; import { createTicketLogContext } from "@/features/logs/utils"; +import { hasTicketStaffAccess } from "@/features/tickets/config-access"; import { getInvitedUserIds, grantTicketParticipantAccess, @@ -28,7 +29,7 @@ import { updateInvitedUserIds } from "@/features/tickets/participants"; import { getOpenTicketByChannel } from "@/features/tickets/records"; -import { getInteractionUser } from "@/features/tickets/utils"; +import { getInteractionUser, getMemberRoleIds } from "@/features/tickets/utils"; export default defineCommand({ data: (LL) => ({ @@ -66,6 +67,17 @@ export default defineCommand({ return; } + if ( + (app.config.tickets.participantsStaffOnly ?? true) && + !hasTicketStaffAccess(app, openTicket.ticketType, getMemberRoleIds(interaction)) + ) { + await reply(app, interaction, { + content: LL.tickets.records.only_staff_manage(), + flags: MessageFlags.Ephemeral + }); + return; + } + const invitedUserIds = getInvitedUserIds(openTicket.ticket); const nextInvitedUserIds = [...invitedUserIds]; const addedUserIds: string[] = []; diff --git a/src/features/commands/remove/command.ts b/src/features/commands/remove/command.ts index 40fcff8d..95fd7ff2 100644 --- a/src/features/commands/remove/command.ts +++ b/src/features/commands/remove/command.ts @@ -24,9 +24,10 @@ import type { TicketRecord } from "@/db/schema"; import { getUserOption } from "@/features/commands/shared/options"; import { sendTicketLog } from "@/features/logs/service"; import { createTicketLogContext } from "@/features/logs/utils"; +import { hasTicketStaffAccess } from "@/features/tickets/config-access"; import { getInvitedUserIds, revokeTicketParticipantAccess, updateInvitedUserIds } from "@/features/tickets/participants"; import { getOpenTicketByChannel } from "@/features/tickets/records"; -import { getInteractionUser } from "@/features/tickets/utils"; +import { getInteractionUser, getMemberRoleIds } from "@/features/tickets/utils"; const REMOVE_USERS_CUSTOM_ID = createCustomId("tickets", "remove-users"); @@ -55,6 +56,17 @@ export default defineCommand({ return; } + if ( + (app.config.tickets.participantsStaffOnly ?? true) && + !hasTicketStaffAccess(app, openTicket.ticketType, getMemberRoleIds(interaction)) + ) { + await reply(app, interaction, { + content: LL.tickets.records.only_staff_manage(), + flags: MessageFlags.Ephemeral + }); + return; + } + const invitedUserIds = getInvitedUserIds(openTicket.ticket); if (invitedUserIds.length === 0) { @@ -121,6 +133,17 @@ export async function handleRemoveUsersSelect(context: ComponentExecutionContext return; } + if ( + (context.app.config.tickets.participantsStaffOnly ?? true) && + !hasTicketStaffAccess(context.app, openTicket.ticketType, getMemberRoleIds(interaction)) + ) { + await updateMessage(context.app, interaction, { + content: context.app.LL.tickets.records.only_staff_manage(), + components: [] + }); + return; + } + const invitedUserIds = getInvitedUserIds(openTicket.ticket); const selectedUserIds = interaction.data.values.filter((userId: string) => invitedUserIds.includes(userId));