Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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 discord.js:在字符串中添加用户名时出现问题_Javascript_Promise_Concatenation_Discord.js - Fatal编程技术网

Javascript discord.js:在字符串中添加用户名时出现问题

Javascript discord.js:在字符串中添加用户名时出现问题,javascript,promise,concatenation,discord.js,Javascript,Promise,Concatenation,Discord.js,我使用Discord.js为Discord机器人编写了以下代码。我试图编写的代码的目的是让bot用另一个.json文件中指定的用户名列表进行回复。这是我写的 if(command === "privlist") { var msg = "" msg += "[Privileged Users]\n" // iterate through the array of id's config.privileged.forEach(function(item, inde

我使用Discord.js为Discord机器人编写了以下代码。我试图编写的代码的目的是让bot用另一个.json文件中指定的用户名列表进行回复。这是我写的

if(command === "privlist")
{
    var msg = ""
    msg += "[Privileged Users]\n"

    // iterate through the array of id's
    config.privileged.forEach(function(item, index) {
        msg += index + ": ";

        // fetch the user associated with the id in the array
        client.fetchUser(item).then(User => {
            // add the name of the user into the string to be outputted
            msg += User.username + "#" + User.discriminator;
        });

        // include the user id as well
        msg += " <" + item + ">\n";
    });

    // send the message
    message.channel.send(msg);
}
if(命令==“privlist”)
{
var msg=“”
msg+=“[特权用户]\n”
//遍历id的数组
config.privileged.forEach(函数(项,索引){
msg+=index+“:”;
//获取与数组中的id关联的用户
client.fetchUser(item).then(User=>{
//将用户名添加到要输出的字符串中
msg+=User.username+“#”+User.discriminator;
});
//还包括用户id
msg+=“\n”;
});
//发送消息
message.channel.send(msg);
}
来自bot的预期回复应该是这样的

[Privileged Users]
0: Merlin#8474 <172734241136836608>
1: Spring Voltage#2118 <255109013383938048>
2: masterhunter56#2561 <243167201471889409>
3: brett#4582 <123957558133129217>
[特权用户]
0:Merlin#8474
1:弹簧电压#2118
2:猎手大师56#2561
3:brett#4582
但相反,这是我得到的

[Privileged Users]
0:  <172734241136836608>
1:  <255109013383938048>
2:  <243167201471889409>
3:  <123957558133129217>
[特权用户]
0:  
1:  
2:  
三:
我尝试在
msg+=User.username+“#”+User.discriminator之后添加
console.log(User.username)
行,这使名称在控制台中正确显示

我甚至可以在
msg+=User.username+“#”+User.discriminator之后执行
message.channel.send(User.username)
将每个名称作为自己的消息发送

我似乎无法将
User.username+“#”+User.discriminator
连接到
msg
字符串。

如前所述,您使用了一个异步函数。这意味着这条线:
msg+=“\n”
不会等待
client.fetchUser(项目)。然后(User
结束以继续

我认为这应该奏效:

if(命令==“privlist”)
{
var msg=“”
msg+=“[特权用户]\n”
//遍历id的数组
config.privileged.forEach(函数(项,索引){
msg+=index+“:”;
//获取与数组中的id关联的用户
client.fetchUser(item).then(User=>{
//将用户名添加到要输出的字符串中
msg+=User.username+“#”+User.discriminator;
//还包括用户id
msg+=“\n”;
});
});
//发送消息
message.channel.send(msg);
}
客户端。获取用户(项)。然后(用户)
是异步的