email fixes
This commit is contained in:
@@ -93,7 +93,25 @@ async function handleButton(interaction) {
|
|||||||
|
|
||||||
// --- CLOSE ---
|
// --- CLOSE ---
|
||||||
if (interaction.customId === 'close_ticket') {
|
if (interaction.customId === 'close_ticket') {
|
||||||
const confirmRow = new ActionRowBuilder().addComponents(
|
const isEmailTicket = ticket.gmailThreadId && !ticket.gmailThreadId.startsWith('discord-');
|
||||||
|
const confirmRow = new ActionRowBuilder();
|
||||||
|
if (isEmailTicket) {
|
||||||
|
confirmRow.addComponents(
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId('confirm_close_with_email')
|
||||||
|
.setLabel('Confirm Close With Email')
|
||||||
|
.setStyle(ButtonStyle.Danger),
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId('confirm_close_no_email')
|
||||||
|
.setLabel('Confirm Close Without Email')
|
||||||
|
.setStyle(ButtonStyle.Danger),
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId('cancel_close')
|
||||||
|
.setLabel('Cancel')
|
||||||
|
.setStyle(ButtonStyle.Secondary)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
confirmRow.addComponents(
|
||||||
new ButtonBuilder()
|
new ButtonBuilder()
|
||||||
.setCustomId('confirm_close')
|
.setCustomId('confirm_close')
|
||||||
.setLabel('Confirm Close')
|
.setLabel('Confirm Close')
|
||||||
@@ -103,6 +121,7 @@ async function handleButton(interaction) {
|
|||||||
.setLabel('Cancel')
|
.setLabel('Cancel')
|
||||||
.setStyle(ButtonStyle.Secondary)
|
.setStyle(ButtonStyle.Secondary)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return interaction.reply({
|
return interaction.reply({
|
||||||
content: 'Are you sure you want to close this ticket?',
|
content: 'Are you sure you want to close this ticket?',
|
||||||
@@ -110,7 +129,12 @@ async function handleButton(interaction) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (interaction.customId === 'confirm_close') {
|
if (
|
||||||
|
interaction.customId === 'confirm_close' ||
|
||||||
|
interaction.customId === 'confirm_close_with_email' ||
|
||||||
|
interaction.customId === 'confirm_close_no_email'
|
||||||
|
) {
|
||||||
|
const sendEmail = interaction.customId !== 'confirm_close_no_email';
|
||||||
const timerSeconds = CONFIG.FORCE_CLOSE_TIMER;
|
const timerSeconds = CONFIG.FORCE_CLOSE_TIMER;
|
||||||
if (pendingCloses.has(interaction.channel.id)) {
|
if (pendingCloses.has(interaction.channel.id)) {
|
||||||
return interaction.reply({ content: 'A close is already pending for this ticket.', ephemeral: true });
|
return interaction.reply({ content: 'A close is already pending for this ticket.', ephemeral: true });
|
||||||
@@ -123,6 +147,7 @@ async function handleButton(interaction) {
|
|||||||
);
|
);
|
||||||
await interaction.update({ content: `Closing ticket in ${timerSeconds} seconds.`, components: [cancelRow] });
|
await interaction.update({ content: `Closing ticket in ${timerSeconds} seconds.`, components: [cancelRow] });
|
||||||
const timerId = setTimeout(async () => {
|
const timerId = setTimeout(async () => {
|
||||||
|
const pending = pendingCloses.get(interaction.channel.id);
|
||||||
pendingCloses.delete(interaction.channel.id);
|
pendingCloses.delete(interaction.channel.id);
|
||||||
const freshTicket = await Ticket.findOne({ discordThreadId: interaction.channel.id }).lean();
|
const freshTicket = await Ticket.findOne({ discordThreadId: interaction.channel.id }).lean();
|
||||||
if (!freshTicket || freshTicket.status === 'closed') return;
|
if (!freshTicket || freshTicket.status === 'closed') return;
|
||||||
@@ -132,9 +157,10 @@ async function handleButton(interaction) {
|
|||||||
{ name: 'Set by', value: interaction.user.tag },
|
{ name: 'Set by', value: interaction.user.tag },
|
||||||
{ name: 'Duration', value: `${timerSeconds}s` }
|
{ name: 'Duration', value: `${timerSeconds}s` }
|
||||||
]).catch(() => {});
|
]).catch(() => {});
|
||||||
await handleConfirmClose(interaction, freshTicket);
|
const effectiveSendEmail = pending?.sendEmail ?? true;
|
||||||
|
await handleConfirmClose(interaction, freshTicket, effectiveSendEmail);
|
||||||
}, timerSeconds * 1000);
|
}, timerSeconds * 1000);
|
||||||
pendingCloses.set(interaction.channel.id, { timeout: timerId, userId: interaction.user.id, username: interaction.user.tag });
|
pendingCloses.set(interaction.channel.id, { timeout: timerId, userId: interaction.user.id, username: interaction.user.tag, sendEmail });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -394,7 +420,7 @@ async function handleClaim(interaction, ticket) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// --- CONFIRM CLOSE ---
|
// --- CONFIRM CLOSE ---
|
||||||
async function handleConfirmClose(interaction, ticket) {
|
async function handleConfirmClose(interaction, ticket, sendEmail = true) {
|
||||||
const closedAt = new Date();
|
const closedAt = new Date();
|
||||||
try {
|
try {
|
||||||
await interaction.update({ content: 'Archiving and closing...', components: [] });
|
await interaction.update({ content: 'Archiving and closing...', components: [] });
|
||||||
@@ -522,8 +548,8 @@ async function handleConfirmClose(interaction, ticket) {
|
|||||||
const closerDisplayName =
|
const closerDisplayName =
|
||||||
interaction.member?.displayName || interaction.user.username;
|
interaction.member?.displayName || interaction.user.username;
|
||||||
|
|
||||||
if (!ticket.gmailThreadId?.startsWith('discord-')) {
|
if (sendEmail && !ticket.gmailThreadId?.startsWith('discord-')) {
|
||||||
await sendTicketClosedEmail(ticket, closerDisplayName);
|
await sendTicketClosedEmail(ticket, closerDisplayName, interaction.user.id);
|
||||||
}
|
}
|
||||||
await Ticket.updateOne(
|
await Ticket.updateOne(
|
||||||
{ gmailThreadId: ticket.gmailThreadId },
|
{ gmailThreadId: ticket.gmailThreadId },
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ async function reloadGmailClient() {
|
|||||||
return { emailAddress: profile.data.emailAddress };
|
return { emailAddress: profile.data.emailAddress };
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sendTicketClosedEmail(ticket, discordDisplayName) {
|
async function sendTicketClosedEmail(ticket, closerName, userId = null) {
|
||||||
try {
|
try {
|
||||||
const gmail = getGmailClient();
|
const gmail = getGmailClient();
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ async function sendTicketClosedEmail(ticket, discordDisplayName) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let subjectHeader = ticket.subject || 'Support';
|
let originalSubject = null;
|
||||||
let msgId = null;
|
let msgId = null;
|
||||||
try {
|
try {
|
||||||
const thread = await gmail.users.threads.get({
|
const thread = await gmail.users.threads.get({
|
||||||
@@ -94,63 +94,74 @@ async function sendTicketClosedEmail(ticket, discordDisplayName) {
|
|||||||
id: ticket.gmailThreadId
|
id: ticket.gmailThreadId
|
||||||
});
|
});
|
||||||
const messages = thread.data.messages || [];
|
const messages = thread.data.messages || [];
|
||||||
const lastMsg = [...messages].reverse()[0];
|
const firstMsg = messages[0];
|
||||||
if (lastMsg?.payload?.headers) {
|
if (firstMsg?.payload?.headers) {
|
||||||
const subj = lastMsg.payload.headers.find(h => h.name === 'Subject')?.value;
|
const subj = firstMsg.payload.headers.find(h => h.name === 'Subject')?.value;
|
||||||
if (subj) subjectHeader = subj;
|
if (subj) originalSubject = subj;
|
||||||
msgId = sanitizeHeaderValue(lastMsg.payload.headers.find(h => h.name === 'Message-ID')?.value);
|
msgId = sanitizeHeaderValue(firstMsg.payload.headers.find(h => h.name === 'Message-ID')?.value);
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {}
|
||||||
/* use ticket.subject and no In-Reply-To if thread fetch fails */
|
|
||||||
|
const baseSubject = originalSubject || ticket.subject || 'Support';
|
||||||
|
const stripped = String(baseSubject).replace(/^(?:\s*Re\s*:\s*)+/i, '');
|
||||||
|
const safeSubject = sanitizeHeaderValue(`Re: ${stripped}`);
|
||||||
|
const utf8Subject = `=?utf-8?B?${Buffer.from(safeSubject).toString('base64')}?=`;
|
||||||
|
|
||||||
|
const messageBody = `${closerName} has marked this ticket as resolved. If you would like to re-open this issue, please reply to this email.`;
|
||||||
|
|
||||||
|
let signatureBlocks = { text: '', html: '' };
|
||||||
|
if (userId) {
|
||||||
|
signatureBlocks = await getStaffSignatureBlocks(userId);
|
||||||
}
|
}
|
||||||
|
// signatureBlocks.html arrives pre-escaped from getStaffSignatureBlocks.
|
||||||
|
const safeStaffSigHtml = signatureBlocks.html ? signatureBlocks.html.replace(/\n/g, '<br>') : '';
|
||||||
|
const safeStaffSigText = signatureBlocks.text;
|
||||||
|
|
||||||
const finalSubject = sanitizeHeaderValue(`${CONFIG.TICKET_CLOSE_SUBJECT_PREFIX} ${subjectHeader}`);
|
|
||||||
const utf8Subject = `=?utf-8?B?${Buffer.from(
|
|
||||||
finalSubject
|
|
||||||
).toString('base64')}?=`;
|
|
||||||
|
|
||||||
const serverDisplayName = escapeHtml(discordDisplayName || CONFIG.SUPPORT_NAME || 'Support');
|
|
||||||
const safeLogoUrl = escapeHtml(CONFIG.LOGO_URL || '');
|
|
||||||
const safeSignature = escapeHtml(CONFIG.SIGNATURE || '').replace(/\n/g, '<br>');
|
|
||||||
const safeCloseMessage = escapeHtml(CONFIG.TICKET_CLOSE_MESSAGE || '').replace(/\n/g, '<br>');
|
|
||||||
const safeCloseSignature = escapeHtml(CONFIG.TICKET_CLOSE_SIGNATURE || '').replace(/\n/g, '<br>');
|
|
||||||
const htmlBody = `
|
const htmlBody = `
|
||||||
<div style="font-family: sans-serif; font-size: 14px; color: #333;">
|
<div style="font-family: sans-serif; font-size: 14px; color: #333;">
|
||||||
<p><strong>From:</strong> ${serverDisplayName} on Discord</p>
|
<p>${escapeHtml(messageBody).replace(/\n/g, '<br>')}</p>
|
||||||
<p><strong>Message:</strong></p>
|
${safeStaffSigHtml ? `<p style="margin: 10px 0;">${safeStaffSigHtml}</p>` : ''}
|
||||||
<p>${safeCloseMessage}</p>
|
${buildCompanySigHtml()}
|
||||||
<p style="margin-top: 16px;">${safeCloseSignature}</p>
|
|
||||||
<hr style="border:none; border-top:1px solid #ddd; margin:20px 0;">
|
|
||||||
<table border="0" cellpadding="0" cellspacing="0">
|
|
||||||
<tr>
|
|
||||||
<td style="padding-right: 12px;">
|
|
||||||
${safeLogoUrl ? `<img src="${safeLogoUrl}" width="65">` : ''}
|
|
||||||
</td>
|
|
||||||
<td style="border-left: 1px solid #ddd; padding-left: 12px;">
|
|
||||||
<p style="margin: 0; font-weight: bold;">${serverDisplayName}</p>
|
|
||||||
<div style="color: #666; font-size: 12px;">${safeSignature}</div>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
</div>`;
|
</div>`;
|
||||||
|
|
||||||
const rawHeaders = [
|
const boundary = '000000000000' + Date.now().toString(16);
|
||||||
|
|
||||||
|
const plainBody = [];
|
||||||
|
plainBody.push(messageBody);
|
||||||
|
if (safeStaffSigText) {
|
||||||
|
plainBody.push('');
|
||||||
|
plainBody.push(safeStaffSigText);
|
||||||
|
}
|
||||||
|
plainBody.push('');
|
||||||
|
plainBody.push(...buildCompanySigText().split('\n'));
|
||||||
|
|
||||||
|
const headers = [
|
||||||
`From: ${sanitizeHeaderValue(CONFIG.MY_EMAIL)}`,
|
`From: ${sanitizeHeaderValue(CONFIG.MY_EMAIL)}`,
|
||||||
`To: ${recipientEmail}`,
|
`To: ${recipientEmail}`,
|
||||||
`Subject: ${utf8Subject}`,
|
`Subject: ${utf8Subject}`,
|
||||||
msgId ? `In-Reply-To: ${msgId}` : '',
|
msgId && `In-Reply-To: ${msgId}`,
|
||||||
msgId ? `References: ${msgId}` : '',
|
msgId && `References: ${msgId}`,
|
||||||
'MIME-Version: 1.0',
|
'MIME-Version: 1.0',
|
||||||
'Content-Type: text/html; charset="UTF-8"',
|
`Content-Type: multipart/alternative; boundary="${boundary}"`
|
||||||
'',
|
|
||||||
htmlBody
|
|
||||||
].filter(Boolean);
|
].filter(Boolean);
|
||||||
|
|
||||||
const raw = Buffer.from(rawHeaders.join('\r\n'))
|
const raw = Buffer.from([
|
||||||
|
...headers,
|
||||||
|
'',
|
||||||
|
`--${boundary}`,
|
||||||
|
'Content-Type: text/plain; charset="UTF-8"',
|
||||||
|
'',
|
||||||
|
...plainBody,
|
||||||
|
'',
|
||||||
|
`--${boundary}`,
|
||||||
|
'Content-Type: text/html; charset="UTF-8"',
|
||||||
|
'',
|
||||||
|
htmlBody,
|
||||||
|
'',
|
||||||
|
`--${boundary}--`
|
||||||
|
].join('\r\n'))
|
||||||
.toString('base64')
|
.toString('base64')
|
||||||
.replace(/\+/g, '-')
|
.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
||||||
.replace(/\//g, '_')
|
|
||||||
.replace(/=+$/, '');
|
|
||||||
|
|
||||||
await gmail.users.messages.send({
|
await gmail.users.messages.send({
|
||||||
userId: 'me',
|
userId: 'me',
|
||||||
|
|||||||
@@ -385,7 +385,7 @@ async function checkAutoClose(client, sendTicketClosedEmail) {
|
|||||||
{ $set: { status: 'closed', pendingDelete: true } }
|
{ $set: { status: 'closed', pendingDelete: true } }
|
||||||
));
|
));
|
||||||
|
|
||||||
await sendTicketClosedEmail(ticket, 'Auto-Close System');
|
await sendTicketClosedEmail(ticket, 'Auto-Close System', null);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
enqueueDelete(channel).then(() => {
|
enqueueDelete(channel).then(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user