diff options
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/gateway/src/commands/pin.ts | 124 |
1 files changed, 74 insertions, 50 deletions
diff --git a/packages/gateway/src/commands/pin.ts b/packages/gateway/src/commands/pin.ts index a6babc3..603c172 100644 --- a/packages/gateway/src/commands/pin.ts +++ b/packages/gateway/src/commands/pin.ts @@ -16,93 +16,117 @@ export const handlePinCommand = async (message: Message) => { if (parameters.length < 2) { await replyWithCleanup( message, - "❌ Usage: `uma!pin <message_id> [channel_id]`", + "❌ Usage: `uma!pin <message_id> [message_id2] [message_id3] ...` or `uma!pin <channel_id> <message_id> [message_id2] ...`", ); return; } - const messageId = parameters[1]; - const channelId = parameters[2]; + let targetChannel = message.channel; + let messageIds: string[] = []; + const firstParameter = parameters[1]; - if (!/^\d{17,19}$/.test(messageId)) { - await replyWithCleanup( - message, - "❌ Invalid message ID format. Please provide a valid Discord message ID.", - ); + if (/^\d{17,19}$/.test(firstParameter)) { + const potentialChannel = message.client.channels.cache.get(firstParameter); - return; + if (potentialChannel && potentialChannel.isTextBased()) { + targetChannel = potentialChannel; + messageIds = parameters.slice(2); + } else { + messageIds = parameters.slice(1); + } + } else { + messageIds = parameters.slice(1); } - if (channelId && !/^\d{17,19}$/.test(channelId)) { + if (messageIds.length === 0) { await replyWithCleanup( message, - "❌ Invalid channel ID format. Please provide a valid Discord channel ID.", + "❌ Please provide at least one message 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) { + for (const messageId of messageIds) + if (!/^\d{17,19}$/.test(messageId)) { await replyWithCleanup( message, - "❌ Message not found in the specified channel.", + "❌ Invalid message ID format. Please provide valid Discord message IDs.", ); return; } - if (targetMessage.pinned) { - await message.delete(); - await targetMessage.unpin(); + try { + const results: { success: boolean; messageId: string; action: string; error?: string }[] = []; + + for (const messageId of messageIds) { + try { + const targetMessage = await targetChannel.messages.fetch(messageId); + + if (!targetMessage) { + results.push({ + success: false, + messageId, + action: "fetch", + error: "Message not found" + }); - if (targetChannel.isTextBased()) { - const replyMessage = await (targetChannel as any).send("📌 Message unpinned successfully."); + continue; + } - setTimeout(async () => { - try { - await replyMessage.delete(); - } catch (error) { - console.error("Failed to delete unpin confirmation message:", error); - } - }, 5000); + if (targetMessage.pinned) { + await targetMessage.unpin(); + results.push({ + success: true, + messageId, + action: "unpin" + }); + } else { + await targetMessage.pin(); + results.push({ + success: true, + messageId, + action: "pin" + }); + } + } catch (error) { + console.error(`Error processing message ${messageId}:`, error); + results.push({ + success: false, + messageId, + action: "process", + error: error instanceof Error ? error.message : "Unknown error" + }); } - return; } await message.delete(); - await targetMessage.pin(); + + const successCount = results.filter(r => r.success).length; + const failCount = results.filter(r => !r.success).length; + let resultMessage = `📌 Processed ${results.length} message(s):\n`; + + resultMessage += `✅ ${successCount} successful, ❌ ${failCount} failed\n\n`; + + for (const result of results) + if (result.success) { + resultMessage += `✅ ${result.messageId}: ${result.action}ned\n`; + } else { + resultMessage += `❌ ${result.messageId}: ${result.error}\n`; + } if (targetChannel.isTextBased()) { - const replyMessage = await (targetChannel as any).send("📌 Message pinned successfully."); + const replyMessage = await (targetChannel as any).send(resultMessage); setTimeout(async () => { try { await replyMessage.delete(); } catch (error) { - console.error("Failed to delete pin confirmation message:", error); + console.error("Failed to delete result message:", error); } - }, 5000); + }, 10000); } } catch (error) { console.error("Error pinning message:", error); |