如何使用DiscordJS v13编写一个命令来重命名当前使用该命令的频道?

huangapple go评论85阅读模式
英文:

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

但是在测试时,它显示交互失败。有人知道如何处理吗?

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

答案1

得分: 2

我认为你的控制台也会收到错误消息,因为你不能在then()之外使用newChannel变量。你已经在使用async,你可以使用await来等待机器人更改频道的名称。

此外,没有channel变量。你是不是指的是interaction.channel?那是交互消息发送的频道。

module.exports = {
  name: 'rename',
  description: '重命名当前频道',
  permission: 'ADMINISTRATOR',

  /**
   *
   * @param {CommandInteraction} interaction
   */
  async execute(interaction) {
    try {
      let newChannel = await interaction.channel.setName('not_general');
      
      console.log(`频道的新名称是 ${newChannel.name}`);

      let changeEmbed = new MessageEmbed()
        .setColor('#5665da')
        .setTitle('票务更新')
        .setDescription(`票务频道名称已设置为 ${newChannel}`)
        .setTimestamp();

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

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

huangapple
  • 本文由 发表于 2023年1月6日 01:04:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75021973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定