Closes the remaining non-broccolini interaction paths after the prior TICKET_BUTTON_HANDLERS gate. After this commit, every bot interaction is staff-only except the panel buttons (open_ticket / open_ticket_thread / open_ticket_channel) and their ticket-creation modal submit — those have to stay public because they're how members and customers open tickets. Specific changes: - handlers/commands/index.js: handleCommand no longer has the `!== 'help'` carve-out. /help now goes through requireStaffRole like every other slash command. Non-staff get the same ephemeral "only available to the support team" reply. - broccolini-discord.js: the signature_modal_* modal-submit handler now calls requireStaffRole before writing to StaffSignature. /signature already gates the modal display via the slash-command staff check; this is defense in depth against directly crafted submissions. - handlers/buttons.js: cancel_delete_tag moved out of FREE_BUTTON_HANDLERS and gated alongside confirm_delete_tag::*. The dialog is only shown ephemerally to the staff who triggered /response delete, so non-staff can't reach it in normal flow; gating keeps the button surface consistent. Kept public (by design — these are the customer entry points): open_ticket / open_ticket_thread / open_ticket_channel buttons ticket_modal / ticket_modal_thread / ticket_modal_channel submits
300 lines
12 KiB
JavaScript
300 lines
12 KiB
JavaScript
/**
|
|
* Slash command, context menu, and autocomplete dispatcher.
|
|
*
|
|
* Submodules own command handlers by topic:
|
|
* helpers.js — requireStaffRole, fetchLoggingChannel
|
|
* escalation.js — runEscalation, runDeescalation, handleEscalate, handleDeescalate
|
|
* close.js — handleForceClose, handleCancelClose, handleCloseTimer (+ finalize/transcript)
|
|
* response.js — /response subcommands + handleAutocomplete
|
|
* panel.js — handlePanel, handleSignature
|
|
* contextMenu.js — handleCreateTicketFromMessage, handleViewUserTickets
|
|
*
|
|
* This file holds the dispatchers, the small "remainder" handlers
|
|
* (channel-mod, settings toggles, /help, /notifydm), and the public
|
|
* module.exports surface that handlers/buttons.js + broccolini-discord.js
|
|
* import from `require('./commands')`.
|
|
*/
|
|
const { EmbedBuilder, MessageFlags } = require('discord.js');
|
|
const { mongoose } = require('../../db-connection');
|
|
const { CONFIG } = require('../../config');
|
|
const { setNotifyDm } = require('../../services/staffSettings');
|
|
const { enqueueMove, enqueueOverwrite, enqueueTopic, enqueueSend } = require('../../services/channelQueue');
|
|
const { logTicketEvent } = require('../../services/debugLog');
|
|
const { findTicketForChannel } = require('../sharedHelpers');
|
|
|
|
const { requireStaffRole, fetchLoggingChannel } = require('./helpers');
|
|
const { runEscalation, runDeescalation, handleEscalate, handleDeescalate } = require('./escalation');
|
|
const { handleCloseTimer, handleCancelClose, handleForceClose } = require('./close');
|
|
const { handleResponse, handleAutocomplete } = require('./response');
|
|
const { handlePanel, handleSignature } = require('./panel');
|
|
const { handleCreateTicketFromMessage, handleViewUserTickets } = require('./contextMenu');
|
|
|
|
const Ticket = mongoose.model('Ticket');
|
|
|
|
// ============================================================
|
|
// Remainder handlers — small enough not to deserve their own module.
|
|
// ============================================================
|
|
|
|
async function handleNotifyDm(interaction) {
|
|
try {
|
|
const setting = interaction.options.getString('setting') === 'on';
|
|
await setNotifyDm(interaction.user.id, interaction.guildId, setting);
|
|
await interaction.reply({
|
|
content: `DM notifications ${setting ? 'enabled ✅' : 'disabled 🔕'}.`,
|
|
flags: MessageFlags.Ephemeral
|
|
});
|
|
} catch (err) {
|
|
console.error('notifydm error:', err);
|
|
await interaction.reply({ content: 'Failed to update notification setting.', flags: MessageFlags.Ephemeral }).catch(() => {});
|
|
}
|
|
}
|
|
|
|
async function handleAdd(interaction) {
|
|
const user = interaction.options.getUser('user');
|
|
const ticket = await findTicketForChannel(interaction);
|
|
if (!ticket) return;
|
|
|
|
try {
|
|
await enqueueOverwrite(interaction.channel, user.id, {
|
|
ViewChannel: true,
|
|
SendMessages: true,
|
|
ReadMessageHistory: true
|
|
});
|
|
await interaction.reply({ content: `Added ${user} to this ticket.`, allowedMentions: { parse: ['users'] } });
|
|
} catch (err) {
|
|
console.error('Add user error:', err);
|
|
await interaction.reply({ content: 'Failed to add user.', flags: MessageFlags.Ephemeral });
|
|
}
|
|
}
|
|
|
|
async function handleRemove(interaction) {
|
|
const user = interaction.options.getUser('user');
|
|
const ticket = await findTicketForChannel(interaction);
|
|
if (!ticket) return;
|
|
|
|
try {
|
|
await enqueueOverwrite(interaction.channel, user.id, null, 'delete');
|
|
await interaction.reply({ content: `Removed ${user} from this ticket.`, allowedMentions: { parse: ['users'] } });
|
|
} catch (err) {
|
|
console.error('Remove user error:', err);
|
|
await interaction.reply({ content: 'Failed to remove user.', flags: MessageFlags.Ephemeral });
|
|
}
|
|
}
|
|
|
|
async function handleTransfer(interaction) {
|
|
const member = interaction.options.getUser('member');
|
|
const reason = interaction.options.getString('reason') || 'No reason provided';
|
|
const ticket = await findTicketForChannel(interaction);
|
|
if (!ticket) return;
|
|
|
|
const staffRoleId = CONFIG.ROLE_TO_PING_ID;
|
|
const guildMember = await interaction.guild.members.fetch(member.id).catch(() => null);
|
|
|
|
if (!guildMember || !guildMember.roles.cache.has(staffRoleId)) {
|
|
return interaction.reply({ content: 'The target member must have the staff role.', flags: MessageFlags.Ephemeral });
|
|
}
|
|
|
|
try {
|
|
const claimerLabel = guildMember.displayName || guildMember.user.username;
|
|
|
|
await Ticket.updateOne(
|
|
{ gmailThreadId: ticket.gmailThreadId },
|
|
{ $set: { claimedBy: claimerLabel } }
|
|
);
|
|
|
|
// `reason` is staff-supplied freeform text; gate to user pings so @everyone in it can't mass-ping.
|
|
await interaction.reply({
|
|
content: `Ticket transferred to ${member} by ${interaction.user}.\nReason: ${reason}`,
|
|
allowedMentions: { parse: ['users'] }
|
|
});
|
|
|
|
const logChan = await fetchLoggingChannel(interaction.client);
|
|
if (logChan) {
|
|
await enqueueSend(logChan, {
|
|
content: `Ticket ${interaction.channel} transferred from ${interaction.user.tag} to ${member.tag}.\nReason: ${reason}`,
|
|
allowedMentions: { parse: ['users'] }
|
|
});
|
|
}
|
|
} catch (err) {
|
|
console.error('Transfer error:', err);
|
|
await interaction.reply({ content: 'Failed to transfer ticket.', flags: MessageFlags.Ephemeral });
|
|
}
|
|
}
|
|
|
|
async function handleMove(interaction) {
|
|
const category = interaction.options.getChannel('category');
|
|
const ticket = await findTicketForChannel(interaction);
|
|
if (!ticket) return;
|
|
|
|
try {
|
|
await enqueueMove(interaction.channel, category.id);
|
|
await interaction.reply(`Moved ticket to **${category.name}**.`);
|
|
|
|
const logChan = await fetchLoggingChannel(interaction.client);
|
|
if (logChan) {
|
|
await enqueueSend(logChan,
|
|
`Ticket ${interaction.channel} moved to category **${category.name}** by ${interaction.user.tag}`
|
|
);
|
|
}
|
|
} catch (err) {
|
|
console.error('Move error:', err);
|
|
await interaction.reply({ content: 'Failed to move ticket.', flags: MessageFlags.Ephemeral });
|
|
}
|
|
}
|
|
|
|
async function handleTopic(interaction) {
|
|
const text = interaction.options.getString('text');
|
|
const ticket = await findTicketForChannel(interaction);
|
|
if (!ticket) return;
|
|
|
|
try {
|
|
await enqueueTopic(interaction.channel, text);
|
|
await interaction.reply('Topic updated successfully.');
|
|
} catch (err) {
|
|
console.error('Topic error:', err);
|
|
await interaction.reply({ content: 'Failed to update topic.', flags: MessageFlags.Ephemeral });
|
|
}
|
|
}
|
|
|
|
async function handleStaffThread(interaction) {
|
|
const sub = interaction.options.getSubcommand();
|
|
if (sub === 'toggle') {
|
|
CONFIG.STAFF_THREAD_ENABLED = !CONFIG.STAFF_THREAD_ENABLED;
|
|
return interaction.reply({ content: `Staff threads are now **${CONFIG.STAFF_THREAD_ENABLED ? 'enabled' : 'disabled'}**.`, flags: MessageFlags.Ephemeral });
|
|
}
|
|
if (sub === 'name') {
|
|
const name = interaction.options.getString('thread_name').slice(0, 100);
|
|
CONFIG.STAFF_THREAD_NAME = name;
|
|
return interaction.reply({ content: `Staff thread name set to **${name}**.`, flags: MessageFlags.Ephemeral });
|
|
}
|
|
if (sub === 'autorole') {
|
|
const enabled = interaction.options.getBoolean('enabled');
|
|
CONFIG.STAFF_THREAD_AUTO_ADD_ROLE = enabled;
|
|
return interaction.reply({ content: `Auto-add role to staff thread is now **${enabled ? 'enabled' : 'disabled'}**.`, flags: MessageFlags.Ephemeral });
|
|
}
|
|
}
|
|
|
|
async function handlePinMessages(interaction) {
|
|
const sub = interaction.options.getSubcommand();
|
|
const enabled = interaction.options.getBoolean('enabled');
|
|
if (sub === 'initial') {
|
|
CONFIG.PIN_INITIAL_MESSAGE_ENABLED = enabled;
|
|
return interaction.reply({ content: `Auto-pin initial message is now **${enabled ? 'enabled' : 'disabled'}**.`, flags: MessageFlags.Ephemeral });
|
|
}
|
|
if (sub === 'escalation') {
|
|
CONFIG.PIN_ESCALATION_MESSAGE_ENABLED = enabled;
|
|
return interaction.reply({ content: `Auto-pin escalation message is now **${enabled ? 'enabled' : 'disabled'}**.`, flags: MessageFlags.Ephemeral });
|
|
}
|
|
if (sub === 'suppress') {
|
|
CONFIG.PIN_SUPPRESS_SYSTEM_MESSAGE = enabled;
|
|
return interaction.reply({ content: `Suppress pin system message is now **${enabled ? 'enabled' : 'disabled'}**.`, flags: MessageFlags.Ephemeral });
|
|
}
|
|
}
|
|
|
|
async function handleGmailPoll(interaction) {
|
|
const requested = parseInt(interaction.options.getString('interval'), 10);
|
|
// Defense-in-depth: the slash command's addChoices already floors at 30s, but
|
|
// clamp the resolved ms here too so any future caller (or skewed input) can't
|
|
// drop below 30s and trip Gmail's per-user quota under sustained load.
|
|
const ms = Math.max(30000, requested * 1000);
|
|
const seconds = ms / 1000;
|
|
// Lazy require — broccolini-discord re-exports this and we'd otherwise cycle.
|
|
const { setGmailPollInterval } = require('../../broccolini-discord');
|
|
setGmailPollInterval(ms);
|
|
logTicketEvent('Gmail poll interval updated', [
|
|
{ name: 'Interval', value: `${seconds}s` },
|
|
{ name: 'Set by', value: interaction.user.tag }
|
|
], interaction).catch(() => {});
|
|
return interaction.reply({ content: `Gmail poll interval set to ${seconds} seconds.`, flags: MessageFlags.Ephemeral });
|
|
}
|
|
|
|
async function handleHelp(interaction) {
|
|
const embed = new EmbedBuilder()
|
|
.setTitle('Ticket System - Commands')
|
|
.setColor(CONFIG.EMBED_COLOR_OPEN)
|
|
.addFields([
|
|
{
|
|
name: 'User Management',
|
|
value: '`/add @user` - Add user to ticket\n`/remove @user` - Remove user from ticket'
|
|
},
|
|
{
|
|
name: 'Ticket Management',
|
|
value: '`/transfer @staff` - Transfer ticket to another staff member\n`/move #category` - Move ticket to another category\n`/force-close` - Force close ticket without confirmation\n`/topic <text>` - Set ticket topic/description'
|
|
},
|
|
{
|
|
name: 'Saved Responses',
|
|
value: '`/response send <name>` - Send saved response\n`/response create|edit|delete|list` - Manage saved responses'
|
|
},
|
|
{
|
|
name: 'Variables (for responses)',
|
|
value: '`{ticket.user}`, `{ticket.email}`, `{ticket.number}`, `{ticket.subject}`, `{staff.name}`, `{server.name}`, `{date}`, `{time}`'
|
|
},
|
|
{
|
|
name: 'Panel System',
|
|
value: '`/panel #channel` - Create a ticket panel for Discord-side tickets'
|
|
},
|
|
{
|
|
name: 'Escalation',
|
|
value: '`/escalate [reason] [tier]` - Escalate ticket (tier 2 or 3, or one step)\n`/deescalate` - De-escalate ticket (tier 3→2 or tier 2→normal)'
|
|
}
|
|
])
|
|
.setFooter({ text: 'Click buttons on ticket messages to claim/close' });
|
|
|
|
await interaction.reply({ embeds: [embed], flags: MessageFlags.Ephemeral });
|
|
}
|
|
|
|
// ============================================================
|
|
// Dispatch tables
|
|
// ============================================================
|
|
|
|
const COMMAND_HANDLERS = {
|
|
escalate: handleEscalate,
|
|
deescalate: handleDeescalate,
|
|
notifydm: handleNotifyDm,
|
|
add: handleAdd,
|
|
remove: handleRemove,
|
|
transfer: handleTransfer,
|
|
move: handleMove,
|
|
staffthread: handleStaffThread,
|
|
pinmessages: handlePinMessages,
|
|
gmailpoll: handleGmailPoll,
|
|
closetimer: handleCloseTimer,
|
|
'cancel-close': handleCancelClose,
|
|
'force-close': handleForceClose,
|
|
topic: handleTopic,
|
|
response: handleResponse,
|
|
signature: handleSignature,
|
|
help: handleHelp,
|
|
panel: handlePanel
|
|
};
|
|
|
|
const CONTEXT_MENU_HANDLERS = {
|
|
'Create Ticket From Message': handleCreateTicketFromMessage,
|
|
'View User Tickets': handleViewUserTickets
|
|
};
|
|
|
|
/**
|
|
* Slash-command dispatcher. Every command is staff-only — including /help,
|
|
* which previously bypassed the role check.
|
|
*/
|
|
async function handleCommand(interaction) {
|
|
if (await requireStaffRole(interaction)) return;
|
|
const handler = COMMAND_HANDLERS[interaction.commandName];
|
|
if (handler) await handler(interaction);
|
|
}
|
|
|
|
/** Context-menu dispatcher. All entries are staff-only. */
|
|
async function handleContextMenu(interaction) {
|
|
if (await requireStaffRole(interaction)) return;
|
|
const handler = CONTEXT_MENU_HANDLERS[interaction.commandName];
|
|
if (handler) await handler(interaction);
|
|
}
|
|
|
|
module.exports = {
|
|
handleCommand,
|
|
handleContextMenu,
|
|
handleAutocomplete,
|
|
runEscalation,
|
|
runDeescalation
|
|
};
|