Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从bot所在的服务器获取服务器所有者_Javascript_Json_Typescript_Discord.js - Fatal编程技术网

Javascript 从bot所在的服务器获取服务器所有者

Javascript 从bot所在的服务器获取服务器所有者,javascript,json,typescript,discord.js,Javascript,Json,Typescript,Discord.js,我试图让Discord bot在一个嵌入中发送所有服务器所有者的ID和用户名。这是我当前的代码: else if (command === '!test'){ const owners = client.guilds.cache.forEach(guild => console.log('<@' + guild.ownerID + '>')); const owners1 = JSON.stringify(owners) con

我试图让Discord bot在一个嵌入中发送所有服务器所有者的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);
}
else if(命令==='!test'){
constowners=client.guilds.cache.forEach(guild=>console.log(“”));
const owners1=JSON.stringify(所有者)
const embed=new Discord.MessageEmbed()
.setColor(“#fc2eff”)
.setTitle('添加BART'的所有者)
.setDescription(所有者1)
.setTimestamp();
msg.channel.send(嵌入);
}
它将ID记录到控制台,这正是我所需要的,但我想在嵌入式而不仅仅是控制台中显示它们

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

使用创建一个包含所有id连接的数组,然后使用将数组连接到单个字符串中。在嵌入中使用此字符串

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);
constownersarray=client.guilds.cache.map(guild=>``);
const embed=new Discord.MessageEmbed()
.setColor(“#fc2eff”)
.setTitle('添加BART'的所有者)
.setDescription(ownersArray.join(',\n'))
.setTimestamp();
msg.channel.send(嵌入);

如果您想获得
所有者
用户名,可以尝试以下方法:

const { username } = await message.client.users.fetch(message.guild.ownerID);

// Optional, you can also use the 'username' variable
const serverOwner = username;
请记住,您必须使您的
执行
方法
异步

你也可以使用公会
缓存
,但这只在
所有者
在线的情况下对我有效。如果他离线了,他就不会再被缓存了,而我只是得到了一个错误

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