从机器人所在的服务器中获取服务器所有者

时间:2021-04-14 18:38:03

标签: javascript json typescript discord.js

我正在尝试让 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。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如果你想获得 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 在线时才对我有用。如果他离线,他就不会再被缓存,我只是收到一个错误

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