[英]“typeError: Cannot read property 'username' of undefined.”
Here is my code for the >top10
command of my discord bot: 这是我的discord机器人的
>top10
命令的代码:
const Discord = require("discord.js");
module.exports.run = async(bot, message, args, con) => {
const top10query = `SELECT user, points, lstmsg FROM scores WHERE guild = '${message.guild.id}' ORDER BY cast(points as SIGNED) ASC LIMIT 10`
//const top10 = con.query(top10query)
const query = querytxt => {
return new Promise((resolve, reject) => {
con.query(querytxt, (err, results, fields) => {
if (err) reject(err);
resolve([results, fields]);
});
});
};
const [results, fields] = await query(top10query);
const map1 = results.map(results => `User: ${(bot.fetchUser(results.user)).username} Messages: ${results.points} Last message: ${results.lstmsg}`);
message.channel.send(map1)
}
module.exports.help = {
name: "top10",
usage: "``prefix`` top10",
description: "top 10 points",
}
The data for "user" is stored as the user ID. “用户”的数据存储为用户ID。 I get 'undefined' when I use the >top10 command.
当我使用> top10命令时,我得到“未定义”。
Any ideas? 有任何想法吗?
EDIT: 编辑:
I've tried replacing 我尝试更换
const map1 = results.map(results => `User: ${(bot.fetchUser(results.user)).username} Messages: ${results.points} Last message: ${results.lstmsg}`);
message.channel.send(map1)
With 同
const map1 = results.map(results => `User: ${(bot.users.get(results.user.id)).username} Messages: ${results.points} Last message: ${results.lstmsg}`);
But I'm getting this error: 但我收到此错误:
(node:21128) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'username' of undefined
Replace 更换
User: ${(bot.fetchUser(results.user)).username
with: 有:
User: ${bot.users.get(results.user).username
You want to do 你想做
const map1 = await results.map(async result => {
const user = await client.fetchUser(result);
return `User: ${user.username} Messages: ${result.points} Last message: ${result.lstmsg}`;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.