thread.members.add is not a function

Hi can anyone help me with this code?
code:
const { getThreadId } = require('../util/database/getThreadId.js');
const { claimTicket } = require('../util/database/claimTicket.js');

module.exports = {
  async handleButton(interaction) {
    if (interaction.customId.startsWith("claim")) {
      const ticketNumber = interaction.customId.split("-")[1];
      console.debug(`Ticket Number: ${ticketNumber}`);

      const row = await getThreadId(ticketNumber);
      const threadId = row ? row.threadId : null;

      if (!threadId) {
        console.error(`No thread ID found for ticket number: ${ticketNumber}`);
        return; 
      }

      // Prova a trovare il thread nella cache
      let thread = interaction.channel.threads.cache.find(x => x.id === threadId);

      // Se non trovato nella cache, prova a recuperarlo dall'API
      if (!thread) {
        try {
          thread = await interaction.channel.threads.fetch(threadId);
          console.log(thread)
        } catch (error) {
          console.error(`Failed to fetch thread with ID ${threadId}: ${error}`);
          await interaction.reply({ content: `Impossibile trovare il thread associato al ticket.`, ephemeral: true });
          return;
        }
      }

      // Aggiungi l'utente al thread
      try {
        await thread.members.add(interaction.user.id);
        await claimTicket(interaction.user.id, ticketNumber);
        await interaction.reply({ content: `Hai reclamato con successo il ticket <#${ticketNumber}>.`, ephemeral: true });
      } catch (error) {
        console.error(`Failed to add user to thread or claim ticket: ${error.stack}`);
        await interaction.reply({ content: `C'è stato un errore nel reclamare il ticket. Riprova più tardi.`, ephemeral: true });
      }
    }
  }
}
Was this page helpful?