简体   繁体   中英

How would I write a command to rename the current channel the command is used in with DiscordJS v13?

I'm trying to create a command that will rename the current channel the command is used in via /rename . On the discord.js docs, it says to just write:

channel
  .setName('not_general')
  .then((newChannel) => console.log(`Channel's new name is ${newChannel.name}`))
  .catch(console.error);

But when testing it says interaction failed. Does anyone know how to go about this?

module.exports = {
  name: 'rename',
  description: 'Renames current channel',
  permission: 'ADMINISTRATOR',

  /**
   *
   * @param {CommandInteraction} interaction
   */
  async execute(interaction) {
    channel
      .setName('not_general')
      .then((newChannel) =>
        console.log(`Channel's new name is ${newChannel.name}`),
      )
      .catch(console.error);

    changeEmbed = new MessageEmbed()
      .setColor('#5665da')
      .setTitle('Ticket Update')
      .setDescription(`Ticket Channel Name Set To ${newChannel}`)
      .setTimestamp();

    interaction.reply({ embeds: [changeEmbed], ephemeral: true });
  },
};

I think you also receive an error message on your console because you can't use the newChannel variable outside your then() . You're already using async you could use await to wait for the bot to change the channel's name.

Also, there is no channel variable. Did you mean interaction.channel ? That's the channel the interaction was sent in.

module.exports = {
  name: 'rename',
  description: 'Renames current channel',
  permission: 'ADMINISTRATOR',

  /**
   *
   * @param {CommandInteraction} interaction
   */
  async execute(interaction) {
    try {
      let newChannel = await interaction.channel.setName('not_general');
      
      console.log(`Channel's new name is ${newChannel.name}`);

      let changeEmbed = new MessageEmbed()
        .setColor('#5665da')
        .setTitle('Ticket Update')
        .setDescription(`Ticket Channel Name Set To ${newChannel}`)
        .setTimestamp();

      interaction.reply({ embeds: [changeEmbed], ephemeral: true });
    } catch (error) {
      console.error(error);
    }
  },
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM