skip to Main Content

I relativ new on discord.js with node. I’m trying to read the first message from a thread, I don’t know, as an array and save it later. Since I would like to pass on the data later via an API.

I’m breaking my crown here.

i have experimented with:

const { ChannelType } = require('discord.js');

client.on('threadCreate', async (thread) => {
    if (thread.type == ChannelType.GuildPublicThread) {
        // When a new forum post is created
        console.log(thread.parentId) // The forum channel ID
        console.log(thread.id) // The forum post ID
        console.log(thread.name) // The name of the forum post
    }
})

but i con´t find a way to get the wohl thread data. Maybe someone can help me out with that?

2

Answers


  1. Chosen as BEST ANSWER

    Maybe thats helps a bit more? really don´t know waht?

    // Require the necessary discord.js classes
    const { Client, Events, GatewayIntentBits, ChannelType } = require('discord.js');
    const { token } = require('./config/token.json');
    
    // Create a new client instance
    const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
    
    // Client error console
    client.on('error', (error) => { console.error('Discord Client Error:', error); });
    
    // When the client is ready, run this code (only once)
    // We use 'c' for the event parameter to keep it separate from the already defined 'client'
    client.once(Events.ClientReady, c => {
        console.log(`Ready! Logged in as ${c.user.tag}`);
    });
    
    // Thread reading
    client.on('threadCreate', async (thread) => {
        if (thread.type === ChannelType.PublicThread) {
          const messages = await thread.messages.fetch();
          const firstMessage = messages.first();
      
          // Access the data of the first message
          console.log(firstMessage.content); // Example: Log the content of the first message
      
          // You can save the necessary data from the first message for later use in your API
          const messageData = {
            content: firstMessage.content,
            author: firstMessage.author.tag,
            // Add more properties as needed
          };
      
          // Pass the messageData to your API or perform further operations
          // ...
        }
      });
    
    
      
    // Log in to Discord with your client's token
    client.login(token);
    

    and after making the post i got this error:

    Discord Client Error: TypeError: Cannot read properties of undefined (reading 'content') at Client. (C:{PATH}srcbot.js:24:32) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)


  2. const { ChannelType } = require('discord.js');
    
    client.on('threadCreate', async (thread) => {
      if (thread.type === ChannelType.GuildPublicThread) {
        const messages = await thread.messages.fetch();
        const firstMessage = messages.first();
    
        // Access the data of the first message
        console.log(firstMessage.content); // Example: Log the content of the first message
    
        // You can save the necessary data from the first message for later use in your API
        const messageData = {
          content: firstMessage.content,
          author: firstMessage.author.tag,
          // Add more properties as needed
        };
    
        // Pass the messageData to your API or perform further operations
        // ...
      }
    });

    Try using this code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search