【问题标题】:How do I make the bot to change a voice channel's name? (Discord.js)如何让机器人更改语音频道名称? (不和谐.js)
【发布时间】:2023-04-05 20:00:01
【问题描述】:

我的想法是让机器人根据命令后的文本更改语音通道名称。例如,命令=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')
        }
    }
});

但是,这会更改写入消息的频道名称,而不是我想要的频道名称。除此之外的一切都让机器人崩溃了,所以我真的不知道。有什么想法吗?

【问题讨论】:

    标签: javascript node.js discord.js bots


    【解决方案1】:

    问题是您要更改message.channel 的名称,而不是ID 为channel 的名称。

    您需要先获取语音通道。您可以使用guild.channels.resolve(ID) 将ID 解析为语音通道对象,一旦解析完成,使用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);
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 2021-05-23
      • 2021-03-14
      • 2019-12-08
      • 2020-12-28
      • 2020-11-07
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多