import { Message } from "discord.js"; import { replyWithCleanup } from "../utilities"; export const handlePinCommand = async (message: Message) => { if (message.author.bot) return; if (!message.content.startsWith("uma!pin")) return; const application = await message.client.application?.fetch(); const ownerId = application?.owner?.id; if (message.author.id !== ownerId) return; const parameters = message.content.split(" "); if (parameters.length < 2) { await replyWithCleanup( message, "❌ Usage: `uma!pin [channel_id]`", ); return; } const messageId = parameters[1]; const channelId = parameters[2]; if (!/^\d{17,19}$/.test(messageId)) { await replyWithCleanup( message, "❌ Invalid message ID format. Please provide a valid Discord message ID.", ); return; } if (channelId && !/^\d{17,19}$/.test(channelId)) { await replyWithCleanup( message, "❌ Invalid channel ID format. Please provide a valid Discord channel ID.", ); return; } try { let targetChannel = message.channel; if (channelId) { const specifiedChannel = message.client.channels.cache.get(channelId); if (!specifiedChannel || !specifiedChannel.isTextBased()) { await replyWithCleanup( message, "❌ Channel not found or is not a text channel.", ); return; } targetChannel = specifiedChannel; } const targetMessage = await targetChannel.messages.fetch(messageId); if (!targetMessage) { await replyWithCleanup( message, "❌ Message not found in the specified channel.", ); return; } if (targetMessage.pinned) { await replyWithCleanup(message, "❌ Message is already pinned."); return; } await message.delete(); await targetMessage.pin(); } catch (error) { console.error("Error pinning message:", error); if ( error instanceof Error && error.message.includes("Missing Permissions") ) { await replyWithCleanup( message, "❌ Missing permissions to pin messages in this channel.", ); } else if ( error instanceof Error && error.message.includes("Maximum number of pins") ) { await replyWithCleanup( message, "❌ Channel has reached maximum number of pinned messages (50). Unpin another message first.", ); } else { await replyWithCleanup( message, "❌ Failed to pin the message. Message ID may be invalid.", ); } } };