簡體   English   中英

如何讓機器人更改語音頻道的名稱? (不和諧.js)

[英]How do I make the bot to change a voice channel's name? (Discord.js)

我的想法是讓機器人根據命令后的文本更改語音通道名稱。 例如,命令=changename p會將人聲通道名稱更改為" Pepper" 但是,我無法讓機器人這樣做。

這是唯一對我有用的代碼:

client.on('message', message =>
{
    if (message.channel.id === '748181582241857657') 
    {
        if(!message.content.startsWith(prefix) || message.author.bot) return;

        const args = message.content.slice(prefix.length).split(/ +/);
        const command = args.shift().toLowerCase();
        const channel = '843111736898617384' 
        //The ID of the vocal channel I want to change name
        const name = message.content.replace('=changename ','')

        if (command === 'changename')
        {
            if (name === 'p')
            message.channel.setName('🌶 Pepper')
        }
    }
});

但是,這會更改寫入消息的頻道的名稱,而不是我想要的頻道名稱。 除此之外的一切都使機器人崩潰了,所以我真的不知道。 任何想法?

問題是您正在更改message.channel的名稱,而不是 ID 為channel的名稱。

您需要先獲取語音通道。 您可以使用guild.channels.resolve(ID)將 ID 解析為語音通道 object 並在解析后使用setName方法更改其名稱。

client.on('message', (message) => {
  if (!message.content.startsWith(prefix) || message.author.bot) return;
  const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();

  if (command === 'changename') {
    if (!args[0])
      return message.channel.send('You must provide a new channel name');

    const voiceChannelID = '843111736898617384';
    const abbr = {
      a: '🥑 Avocado',
      b: '🍌 Banana',
      c: '🥥 Coconut',
      d: '🌴 Date',
      k: '🥝 Kiwi',
      p: '🌶 Pepper',
    };
    const name = abbr[args[0]];

    if (!name)
      return message.channel.send(`${args[0]} is not a valid channel name`);

    const voiceChannel = message.guild.channels.resolve(voiceChannelID);
    if (!voiceChannel)
      return message.channel.send(
        `Can't find a voice channel with the ID \`${voiceChannelID}\``,
      );

    voiceChannel
      .setName(name)
      .then((newChannel) =>
        message.channel.send(`The channel's new name is ${newChannel.name}`),
      )
      .catch(console.error);
  }
});

暫無
暫無

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

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