Home > other >  Discord.js creates thread but won't add members during command
Discord.js creates thread but won't add members during command

Time:01-26

I am building a Discord bot which, upon running a command, will create a private thread and add the user who ran the command to the thread. I'm running into a problem where the thread is successfully created, but when trying to add a member to the thread, I get an error.

My code is as follows:

const { SlashCommandBuilder, EmbedBuilder } = require('discord.js')

module.exports = {
  data: new SlashCommandBuilder()
    .setName('pthread')
    .setDescription('Create private thread'),

  async execute(interaction) {

    const channel = interaction.channel
    const user = interaction.user
    const userName = user.username
    const threadName = userName   "'s-Private-Thread"

    // Create a new private thread
    channel.threads
      .create({
        name: threadName,
        autoArchiveDuration: 60,
        type: 12,
        reason: 'na',
      })
      .then(threadChannel => console.log(threadChannel))
      .catch(console.error);

    var thread = channel.threads.cache.find(x => x.name === threadName)
    await thread.members.add(user)

    await interaction.reply({ content: 'A private thread has been created for you!', ephemeral: true })

  }
}

Here is the error I am getting:

TypeError: Cannot read properties of undefined (reading 'members')
    at Object.execute (C:\Users\Liam\Desktop\Files\Code\GitHubRepos\ThreadMaker\commands\addEntry.js:27:18)

I get a similar error if I try to use

  await thread.send({ content: 'Hello' })

specifically,

TypeError: Cannot read properties of undefined (reading 'send')
    at Object.execute (C:\Users\Liam\Desktop\Files\Code\GitHubRepos\ThreadMaker\commands\addEntry.js:27:18)

If I run the command twice, it does successfully add the user to the thread/send a message in the thread, but then creates a duplicate thread. Its almost like the command needs to finish executing before I am able to do anything with the thread. My only theory is the thread not saved to the cache before the command finishes executing. If thats the case, can I force it to save to the cache mid execute? Thanks

CodePudding user response:

I'm not sure why you mix then() methods with async/await. Probably that's the reason you think the cache is already updated. However, in your code channel.threads.cache.find() runs before channel.threads.create() finishes creating your thread (because a promise doesn't block the event loop).

channel.threads.create() returns a promise and once it's resolved, the created ThreadChannel. It mean you could grab the created channel instead.

You should also use enums (like ChannelType.GuildPrivateThread) instead of magic numbers (12).

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

// ...

async execute(interaction) {
  const channel = interaction.channel;
  const user = interaction.user;
  const userName = user.username;
  const threadName = userName   "'s-Private-Thread";

  try {
    // Create a new private thread
    const threadChannel = await channel.threads.create({
      name: threadName,
      autoArchiveDuration: 60,
      type: ChannelType.GuildPrivateThread,
      reason: 'na',
    });

    await threadChannel.members.add(user);

    await interaction.reply({
      content: 'A private thread has been created for you!',
      ephemeral: true,
    });
  } catch (error) {
    console.log(error);
  }
},
  • Related