42 lines
1.5 KiB
JavaScript
42 lines
1.5 KiB
JavaScript
/**
|
|
* Auto-pin utility — pins a message with error handling and optional
|
|
* system message suppression.
|
|
*
|
|
* Discord rate-limits pin operations to approximately 5 per second per
|
|
* channel. Since pins only happen on ticket creation and escalation (low
|
|
* frequency), no additional rate limiting is needed. The bot requires
|
|
* MANAGE_MESSAGES permission to pin — if this is missing, the pin will
|
|
* fail with code 50013 and be caught by the catch block.
|
|
*/
|
|
const { CONFIG } = require('../config');
|
|
const { logWarn } = require('./debugLog');
|
|
|
|
/**
|
|
* Pin a message in a channel.
|
|
* @param {import('discord.js').Message} message
|
|
* @param {import('discord.js').Client} client
|
|
*/
|
|
async function pinMessage(message, client) {
|
|
try {
|
|
await message.pin();
|
|
|
|
if (CONFIG.PIN_SUPPRESS_SYSTEM_MESSAGE) {
|
|
await new Promise(r => setTimeout(r, 1000));
|
|
const systemMessages = await message.channel.messages.fetch({ limit: 5 });
|
|
const pinNotice = systemMessages.find(m =>
|
|
m.type === 6 && // MessageType.ChannelPinnedMessage
|
|
Date.now() - m.createdTimestamp < 10000
|
|
);
|
|
if (pinNotice) await pinNotice.delete().catch(() => {});
|
|
}
|
|
} catch (err) {
|
|
if (err.code === 30003) {
|
|
await logWarn('pinMessage', `Max pins reached in channel #${message.channel.name} — could not pin message.`, client).catch(() => {});
|
|
} else {
|
|
await logWarn('pinMessage', `Failed to pin message in #${message.channel.name}: ${err.message}`, client).catch(() => {});
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = { pinMessage };
|