Compare commits
3 Commits
840b6bfcf8
...
e3b3b8d48c
| Author | SHA1 | Date | |
|---|---|---|---|
| e3b3b8d48c | |||
| 3ac23466b2 | |||
| 83b6b4ae0c |
@@ -18,8 +18,8 @@ const CONFIG = {
|
|||||||
DISCORD_TICKET_CATEGORY_ID: process.env.DISCORD_TICKET_CATEGORY_ID || process.env.TICKET_CATEGORY_ID,
|
DISCORD_TICKET_CATEGORY_ID: process.env.DISCORD_TICKET_CATEGORY_ID || process.env.TICKET_CATEGORY_ID,
|
||||||
ROLE_ID_TO_PING: process.env.ROLE_ID_TO_PING,
|
ROLE_ID_TO_PING: process.env.ROLE_ID_TO_PING,
|
||||||
ROLE_TO_PING_ID: process.env.ROLE_ID_TO_PING || process.env.ROLE_TO_PING_ID,
|
ROLE_TO_PING_ID: process.env.ROLE_ID_TO_PING || process.env.ROLE_TO_PING_ID,
|
||||||
TRANSCRIPT_CHAN: process.env.TRANSCRIPT_CHANNEL_ID,
|
TRANSCRIPT_CHANNEL_ID: process.env.TRANSCRIPT_CHANNEL_ID,
|
||||||
LOG_CHAN: process.env.LOGGING_CHANNEL_ID,
|
LOGGING_CHANNEL_ID: process.env.LOGGING_CHANNEL_ID,
|
||||||
DEBUGGING_CHANNEL_ID: process.env.DEBUGGING_CHANNEL_ID || null,
|
DEBUGGING_CHANNEL_ID: process.env.DEBUGGING_CHANNEL_ID || null,
|
||||||
CLIENT_ID: process.env.DISCORD_APPLICATION_ID,
|
CLIENT_ID: process.env.DISCORD_APPLICATION_ID,
|
||||||
REFRESH_TOKEN: process.env.REFRESH_TOKEN,
|
REFRESH_TOKEN: process.env.REFRESH_TOKEN,
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ const {
|
|||||||
stripEmailQuotes,
|
stripEmailQuotes,
|
||||||
stripMobileFooter,
|
stripMobileFooter,
|
||||||
detectGame,
|
detectGame,
|
||||||
enforceEmbedLimit,
|
|
||||||
sanitizeEmbedText
|
sanitizeEmbedText
|
||||||
} = require('./utils');
|
} = require('./utils');
|
||||||
const { getGmailClient } = require('./services/gmail');
|
const { getGmailClient } = require('./services/gmail');
|
||||||
@@ -225,7 +224,6 @@ async function poll(client) {
|
|||||||
{ name: 'Subject', value: `\`\`\`\n${sanitizeEmbedText(subject) || 'No subject'}\n\`\`\``, inline: false }
|
{ name: 'Subject', value: `\`\`\`\n${sanitizeEmbedText(subject) || 'No subject'}\n\`\`\``, inline: false }
|
||||||
);
|
);
|
||||||
|
|
||||||
enforceEmbedLimit([ticketInfoEmbed]);
|
|
||||||
const welcomeMsg = await enqueueSend(ticketChan, {
|
const welcomeMsg = await enqueueSend(ticketChan, {
|
||||||
content: `<@&${CONFIG.ROLE_ID_TO_PING}>`,
|
content: `<@&${CONFIG.ROLE_ID_TO_PING}>`,
|
||||||
embeds: [ticketInfoEmbed],
|
embeds: [ticketInfoEmbed],
|
||||||
@@ -251,7 +249,7 @@ async function poll(client) {
|
|||||||
|
|
||||||
if (transcriptRows.length > 0) {
|
if (transcriptRows.length > 0) {
|
||||||
const transcriptChan = await client.channels
|
const transcriptChan = await client.channels
|
||||||
.fetch(CONFIG.TRANSCRIPT_CHAN)
|
.fetch(CONFIG.TRANSCRIPT_CHANNEL_ID)
|
||||||
.catch(() => null);
|
.catch(() => null);
|
||||||
|
|
||||||
if (transcriptChan) {
|
if (transcriptChan) {
|
||||||
|
|||||||
1067
handlers/buttons.js
1067
handlers/buttons.js
File diff suppressed because it is too large
Load Diff
1646
handlers/commands.js
1646
handlers/commands.js
File diff suppressed because it is too large
Load Diff
53
handlers/sharedHelpers.js
Normal file
53
handlers/sharedHelpers.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
* Shared helpers for slash-command and button handlers.
|
||||||
|
*
|
||||||
|
* Both handlers/commands.js and handlers/buttons.js use these to avoid
|
||||||
|
* repeating the lookup-and-defer-and-try-catch pattern across 30+ branches.
|
||||||
|
*/
|
||||||
|
const { mongoose } = require('../db-connection');
|
||||||
|
const { logError } = require('../services/debugLog');
|
||||||
|
|
||||||
|
const Ticket = mongoose.model('Ticket');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Look up the ticket linked to this channel; reply with `missingMessage`
|
||||||
|
* (default: "This channel is not linked to a ticket.") and return null if
|
||||||
|
* the channel is not a ticket. Returns the ticket on success.
|
||||||
|
*
|
||||||
|
* @param {import('discord.js').Interaction} interaction
|
||||||
|
* @param {string} [missingMessage]
|
||||||
|
*/
|
||||||
|
async function findTicketForChannel(interaction, missingMessage = 'This channel is not linked to a ticket.') {
|
||||||
|
const ticket = await Ticket.findOne({ discordThreadId: interaction.channel.id }).lean();
|
||||||
|
if (!ticket) {
|
||||||
|
await interaction.reply({ content: missingMessage, ephemeral: true });
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return ticket;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defer + run + log + reply on error. `verb` is the user-facing verb
|
||||||
|
* (e.g. "escalate"); error messages render as "Failed to <verb> this ticket."
|
||||||
|
* Errors are logged to console + DEBUGGING_CHANNEL_ID via logError(verb, ...).
|
||||||
|
*
|
||||||
|
* @param {import('discord.js').Interaction} interaction
|
||||||
|
* @param {string} verb
|
||||||
|
* @param {() => Promise<void>} fn
|
||||||
|
* @param {{ ephemeral?: boolean }} [opts]
|
||||||
|
*/
|
||||||
|
async function runDeferred(interaction, verb, fn, { ephemeral = false } = {}) {
|
||||||
|
try {
|
||||||
|
await interaction.deferReply({ ephemeral });
|
||||||
|
await fn();
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`${verb} error:`, err);
|
||||||
|
logError(verb, err, interaction).catch(() => {});
|
||||||
|
const msg = `Failed to ${verb} this ticket.`;
|
||||||
|
await interaction.editReply({ content: msg }).catch(() =>
|
||||||
|
interaction.followUp({ content: msg, ephemeral: true }).catch(() => {})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { findTicketForChannel, runDeferred };
|
||||||
@@ -69,7 +69,7 @@ async function logTicketEvent(action, fields, interaction = null) {
|
|||||||
if (interaction?.user?.tag) {
|
if (interaction?.user?.tag) {
|
||||||
embed.setFooter({ text: interaction.user.tag });
|
embed.setFooter({ text: interaction.user.tag });
|
||||||
}
|
}
|
||||||
await sendToChannel(CONFIG.LOG_CHAN, embed, interaction?.client);
|
await sendToChannel(CONFIG.LOGGING_CHANNEL_ID, embed, interaction?.client);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
74
utils.js
74
utils.js
@@ -264,83 +264,9 @@ function truncateEmbedDescription(str, max = 4096) {
|
|||||||
return s.length > max ? s.slice(0, max - 3) + '...' : s;
|
return s.length > max ? s.slice(0, max - 3) + '...' : s;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Enforce the 6 000 char total embed limit across an array of EmbedBuilder
|
|
||||||
* instances. Mutates in place: trims the largest description first, then
|
|
||||||
* largest field values, until the total is under 6 000 chars.
|
|
||||||
* Returns the same array for chaining.
|
|
||||||
*/
|
|
||||||
function enforceEmbedLimit(embeds) {
|
|
||||||
const charCount = (e) => {
|
|
||||||
const d = e.data || {};
|
|
||||||
let total = 0;
|
|
||||||
if (d.title) total += d.title.length;
|
|
||||||
if (d.description) total += d.description.length;
|
|
||||||
if (d.footer?.text) total += d.footer.text.length;
|
|
||||||
if (d.author?.name) total += d.author.name.length;
|
|
||||||
if (d.fields) {
|
|
||||||
for (const f of d.fields) {
|
|
||||||
if (f.name) total += f.name.length;
|
|
||||||
if (f.value) total += f.value.length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
};
|
|
||||||
|
|
||||||
const LIMIT = 6000;
|
|
||||||
|
|
||||||
const totalChars = () => embeds.reduce((sum, e) => sum + charCount(e), 0);
|
|
||||||
|
|
||||||
// Trim largest descriptions first
|
|
||||||
while (totalChars() > LIMIT) {
|
|
||||||
let largestIdx = -1;
|
|
||||||
let largestLen = 0;
|
|
||||||
for (let i = 0; i < embeds.length; i++) {
|
|
||||||
const desc = embeds[i].data?.description;
|
|
||||||
if (desc && desc.length > largestLen) {
|
|
||||||
largestLen = desc.length;
|
|
||||||
largestIdx = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (largestIdx === -1 || largestLen <= 4) break;
|
|
||||||
const excess = totalChars() - LIMIT;
|
|
||||||
const newLen = Math.max(1, largestLen - excess - 3);
|
|
||||||
embeds[largestIdx].setDescription(
|
|
||||||
embeds[largestIdx].data.description.slice(0, newLen) + '...'
|
|
||||||
);
|
|
||||||
if (totalChars() <= LIMIT) break;
|
|
||||||
// If still over, loop will pick next largest
|
|
||||||
}
|
|
||||||
|
|
||||||
// Trim largest field values
|
|
||||||
while (totalChars() > LIMIT) {
|
|
||||||
let targetEmbed = null;
|
|
||||||
let targetFieldIdx = -1;
|
|
||||||
let targetLen = 0;
|
|
||||||
for (const e of embeds) {
|
|
||||||
const fields = e.data?.fields || [];
|
|
||||||
for (let fi = 0; fi < fields.length; fi++) {
|
|
||||||
if (fields[fi].value && fields[fi].value.length > targetLen) {
|
|
||||||
targetLen = fields[fi].value.length;
|
|
||||||
targetEmbed = e;
|
|
||||||
targetFieldIdx = fi;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!targetEmbed || targetLen <= 4) break;
|
|
||||||
const excess = totalChars() - LIMIT;
|
|
||||||
const newLen = Math.max(1, targetLen - excess - 3);
|
|
||||||
targetEmbed.data.fields[targetFieldIdx].value =
|
|
||||||
targetEmbed.data.fields[targetFieldIdx].value.slice(0, newLen) + '...';
|
|
||||||
}
|
|
||||||
|
|
||||||
return embeds;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
sanitizeEmbedText,
|
sanitizeEmbedText,
|
||||||
truncateEmbedDescription,
|
truncateEmbedDescription,
|
||||||
enforceEmbedLimit,
|
|
||||||
escapeHtml,
|
escapeHtml,
|
||||||
safeEqual,
|
safeEqual,
|
||||||
isStaff,
|
isStaff,
|
||||||
|
|||||||
Reference in New Issue
Block a user