簡體   English   中英

我將如何編寫命令來重命名命令用於 DiscordJS v13 的當前頻道?

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

我正在嘗試創建一個命令,該命令將通過/rename命令在其中使用的當前頻道。 在 discord.js 文檔中,它說只寫:

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

但是測試的時候說交互失敗。 有誰知道這個怎么到go?

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 });
  },
};

我認為您還會在控制台上收到一條錯誤消息,因為您不能在then()之外使用newChannel變量。 您已經在使用async ,您可以使用await來等待機器人更改頻道名稱。

此外,沒有channel變量。 您指的是interaction.channel嗎? 這是發送交互的渠道。

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);
    }
  },
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM