NodeJS 未捕获的引用错误引用错误:未定义通道

fdx2calv  于 2023-03-22  发布在  Node.js
关注(0)|答案(1)|浏览(213)

我在编写一个不和谐机器人
我想知道如何在刚刚创建的频道上发送消息
所以我尝试了.then,但它不起作用,所以我不明白为什么:/
我尝试了很多东西,只是访问网络或在YouTube上看视频,但没有人工作
下面是我的代码:

client.on("interactionCreate", async interaction =>  {
    if(interaction.isCommand()){
        if(interaction.commandName == "ticket"){

            const Embed = new EmbedBuilder()
                .setColor(0x0099FF)
                .setTitle('Do you need help ?')
                .setDescription('By clicking on the button,\nModerators will answer your questions/reports!')
                .setFooter({ text: 'Created by Skorflex#9141', iconURL: 'https://cdn.discordapp.com/attachments/1082613588507766784/1082613661140537404/logo-transparent.png' });
            
            const row = new ActionRowBuilder()
                .addComponents(
                    new ButtonBuilder()
                        .setCustomId('button')
                        .setLabel('Open a ticket')
                        .setStyle(ButtonStyle.Primary),
                );

            await interaction.reply({ embeds: [Embed], components: [row] });

        }
    }
    if(interaction.isButton()){
        if(interaction.customId === "button"){

            const server = client.guilds.cache.get(guildId);

            

            server.channels.create({name:`ticket-${interaction.user.username}`})
            .then(channel => {
                let category = client.channels.cache.get(ticket_category);

                if (!category) throw new Error("Category channel does not exist");
                    channel.setParent(category.id);
                }).catch(console.error);

                interaction.reply({content: "Your ticket is available !", ephemeral: true})

                const Embed = new EmbedBuilder()
                .setColor(0x0099FF)
                .setTitle('Thank you for contacting support')
                .setDescription('Describe your problem')

                const row = new ActionRowBuilder()
                .addComponents(
                    new ButtonBuilder()
                        .setCustomId('closebutton')
                        .setLabel('Close the ticket')
                        .setStyle(ButtonStyle.Primary),
                );
                
                channel.send({embeds:[Embed], row:[row]})
        }
    }
})
ffx8fchx

ffx8fchx1#

channel在:

channel.send({embeds:[Embed], row:[row]})

因为您在.then块之外调用此行。
你可以做的是声明一个变量scoped到你的条件。因为你已经使用了async/await,你可以使用它来代替.then,并使用await解析你的promise:

let channel; // defined in the same scope as "channel.send()"
  try {
    channel = await server.channels.create({
      name: `ticket-${interaction.user.username}`,
    });
    ...
  } catch (error) {
    console.error(error);
  }

完整片段:

if (interaction.customId === "button") {
  const server = client.guilds.cache.get(guildId);
  let channel;
  try {
    channel = await server.channels.create({
      name: `ticket-${interaction.user.username}`,
    });
    let category = client.channels.cache.get(ticket_category);

    if (!category) throw new Error("Category channel does not exist");
    channel.setParent(category.id);
  } catch (error) {
    console.error(error);
  }
  interaction.reply({
    content: "Your ticket is available !",
    ephemeral: true,
  });

  const Embed = new EmbedBuilder()
    .setColor(0x0099ff)
    .setTitle("Thank you for contacting support")
    .setDescription("Describe your problem");

  const row = new ActionRowBuilder().addComponents(
    new ButtonBuilder()
      .setCustomId("closebutton")
      .setLabel("Close the ticket")
      .setStyle(ButtonStyle.Primary)
  );

  channel.send({ embeds: [Embed], row: [row] });
}

相关问题