【问题标题】:Get server owners from what servers the bot is in从机器人所在的服务器获取服务器所有者
【发布时间】:2021-07-09 19:59:20
【问题描述】:

我正在尝试让 Discord 机器人在一个嵌入中发送所有服务器所有者的 ID 和用户名。这是我当前的代码:

else if (command === '!test'){
        const owners = client.guilds.cache.forEach(guild => console.log('<@' + guild.ownerID + '>'));
        const owners1 = JSON.stringify(owners)

        const embed = new Discord.MessageEmbed()
                .setColor('#fc2eff')
                .setTitle('Owners who added bartt')
                .setDescription(owners1)
                .setTimestamp();
        
            msg.channel.send(embed);
}

它将 id 记录到控制台,这是我需要的,但我想在嵌入中显示它们,而不仅仅是控制台。

我宁愿记录 Discord 名称而不是 id,因为 Discord 并不总是显示用户的 id。

【问题讨论】:

    标签: javascript json typescript discord.js


    【解决方案1】:

    使用Iterable#map() 创建一个包含所有ID 连接的数组,然后使用Array#join() 将该数组连接成一个字符串。在嵌入中使用此字符串。

    const ownersArray = client.guilds.cache.map(guild => `<@${guild.ownerID}>`);
    
    const embed = new Discord.MessageEmbed()
        .setColor('#fc2eff')
        .setTitle('Owners who added bartt')
        .setDescription(ownersArray.join(',\n'))
        .setTimestamp();
            
    msg.channel.send(embed);
    

    【讨论】:

      【解决方案2】:

      如果你想获得owners 用户名,你可以试试这个:

      const { username } = await message.client.users.fetch(message.guild.ownerID);
      
      // Optional, you can also use the 'username' variable
      const serverOwner = username;
      

      请记住,您必须将execute 方法设为async

      您也可以使用公会cache,但这仅在owner 在线时对我有效。如果他下线了,他就不再被缓存了,我只是得到了一个错误

      我不知道你说的多个所有者是什么意思,因为只有一个,创建服务器的那个

      【讨论】:

        猜你喜欢
        • 2021-02-13
        • 2020-06-26
        • 2021-09-24
        • 2022-01-10
        • 2021-02-02
        • 1970-01-01
        • 2021-08-21
        • 1970-01-01
        • 2022-07-01
        相关资源
        最近更新 更多