HARPA.AI
LIBRARYUSE CASESGUIDESAI COMMANDSBLOG

🧩  Discord Chat Extraction

Extracts messages from Discord discussions. #extraction

Created by Adrian Larsson
Updated on Nov 9, 04:32
Installed 65 times
RUNS JS CODE

How to Use

IMPORT COMMAND

Content

- type: ask
  param: targetMessageCount
  message: >-
    📌 You can use this command or JS code as a base for creating other commands
    or automations.


    How many messages would you like to extract?
  options:
    - label: 50 messages
      value: 50
    - label: 100 messages
      value: 100
    - label: 200 messages
      value: 200
    - $custom
  default: ''
  vision:
    enabled: false
    mode: area
    hint: ''
    send: true
  optionsInvalid: false
- type: js
  args: targetMessageCount
  code: |
    async function scrollAndCollectMessages (targetMessageCount) {
        const config = {
          scroller: '[class*="scroller_"]',
          messageItem: '[class^="messageListItem_"]',
          timestamp: '[class^="timestamp_"] time',
          username: '[class^="username_"]',
          content: '[class^="markup_"][class*="messageContent_"]',
          reply: '[class^="repliedMessage_"]',
        }

        async function collectMessages () {
          const messages = []
          const uniqueMessageIds = new Set()
          let retries = 0
          const maxRetries = 5

          async function scrollAndWait () {
            const elements = document.querySelectorAll(config.scroller)
            const scrollableElement = elements[elements.length - 1]

            if (scrollableElement) {
              scrollableElement.scrollBy(0, -2000)
              await new Promise(resolve => setTimeout(resolve, 200))
            }
          }

          function extractCurrentMessages () {
            const messageElements = document.querySelectorAll(config.messageItem)
            let newMessagesFound = false

            messageElements.forEach(message => {
              const messageId =
                message.id || message.getAttribute('data-list-item-id')

              if (uniqueMessageIds.has(messageId)) return

              const timestamp =
                message.querySelector(config.timestamp)?.textContent || ''
              const username =
                message.querySelector(config.username)?.textContent || ''
              const content =
                message.querySelector(config.content)?.textContent || ''
              const replyElement = message.querySelector(config.reply)
              const replyTo = replyElement
                ? replyElement.querySelector(config.username)?.textContent || ''
                : null

              let formattedMessage = `${timestamp}: ${username}: ${content}`
              if (replyTo) {
                formattedMessage += ` (Replied to: ${replyTo})`
              }

              uniqueMessageIds.add(messageId)
              messages.push(formattedMessage)
              newMessagesFound = true
            })

            return newMessagesFound
          }

          while (messages.length < targetMessageCount && retries < maxRetries) {
            const newMessagesFound = extractCurrentMessages()

            if (messages.length < targetMessageCount) {
              await scrollAndWait()
              if (!newMessagesFound) {
                retries++
              } else {
                retries = 0
              }
            }
          }

          if (messages.length > targetMessageCount) {
            messages.splice(targetMessageCount)
          }

          return messages
        }

        try {
          const messages = await collectMessages()
          const url = window.location.href
          return { messages, url }
        } catch (error) {
          return null
        }
      }

      return scrollAndCollectMessages(targetMessageCount)
  param: messages
  timeout: 150000
  silent: true
- type: say
  message: |-
    **Array:**

    {{messages}}
Notice: Please read before using

This automation command is created by a community member. HARPA AI team does not audit community commands.

Please review the command carefully and only install if you trust the creator.

Contact us
HomeUse CasesGuidesPrivacy PolicyTerms of Service
CAN WE STORE COOKIES?
Our website uses cookies for the purposes of accessibility and security. They also allow us to gather statistics in order to improve the website for you. More info: Privacy Policy