【发布时间】:2018-05-08 19:34:23
【问题描述】:
我正在尝试构建一个具有积分系统和积分最高用户排行榜的机器人。不幸的是,每次有人调用命令时,我都无法提及排行榜上的前 5 名玩家。我试过 msg.author.id 和 msg.author.username 似乎无法记录所需的信息。 .id 只记录一系列数字,以后可以使用 来提及用户,但 .username 始终未定义。我想知道我是否错误地使用了 .username 函数,或者是否有另一种方法可以做到这一点。我还尝试通过 ```` 在代码文本中使用提及功能,虽然它删除了提及,但它并没有显示用户的姓名,只显示数字。
sql.get(`SELECT * FROM scores WHERE userId ="${msg.author.id}"`).then(row => { // Level System
if (!row) {
sql.run("INSERT INTO scores (userId, daily, points, level, glimmer, wr, rotw, name) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", [msg.author.id, 0, 0, 0, 500, 0, 0, msg.author.username]);
//msg.channel.send(`Welcome <@${msg.author.id}>! Thank you for joining Playing Destiny Fast! You start off with 500 glimmer!`);
console.log("new player added to database");
} else {
sql.run(`UPDATE scores SET points = ${row.points + 1} WHERE userId = ${msg.author.id}`);
let curLevel = Math.floor(0.02 * row.points + 1);
if (row.level > 100 || curLevel > 100) { row.level = 100; msg.channel.send(`<@${msg.author.id}> has already achieved the highest level! Thank you for your continued support!`) }
if (curLevel > row.level) {
row.level = curLevel;
sql.run(`UPDATE scores SET points = ${row.points + 1}, level = ${row.level} WHERE userId = ${msg.author.id}`);
msg.reply(`has reached level ${curLevel}!`);
}
}
}).catch(() => {
console.error;
console.log("Table doesn't exist. Compiling...");
sql.run("CREATE TABLE IF NOT EXISTS scores (userId TEXT, daily INTEGER, points INTEGER, level INTEGER, glimmer INTEGER, wr INTEGER, rotw INTEGER, name TEXT)").then(() => {
sql.run("INSERT INTO scores (userId, daily, points, level, glimmer, wr, rotw, name) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", [msg.author.id, 0, 0, 0, 500, 0, 0, msg.author.username]);
msg.channel.send(`<@Programmer> I've encountered a critical error in the database. Please ensure that there is nothing wrong with my primary objective.`);
});
});
if (cat === "level") {
sql.all(`SELECT userId, level FROM scores ORDER BY level DESC LIMIT ${amount}`).then(rows => {
tops.length = 0;
rows.forEach(function (row) {
tops.push(`\n ${row.name} -- ${row.level}`);
});
embed = new Discord.RichEmbed()
.setAuthor(`Failsafe`)
.setColor(0x7EC0EE)
.setThumbnail("https://i.imgur.com/1XPQkeC.jpg")
.setDescription("---------------------------------------------------------------")
.setFooter("")
.addField(StringFormat(`Top ${amount} Users: ${cat}`, [NumShown]), "```" + tops.join('') + "```");
msg.channel.send(embed);
});
}
我正在使用 Microsoft Visual Studios 2017、Node.js 8x、discord.js、cheerio、sqlite、node-pre-gyp 和 sqlite 3 作为主要库。我在 Windows 10 上使用单个 sqlite 文件将收集的数据存储到只有行和列的单个表中。我没有收到任何错误消息,只是输出不正确。它当前输出用户的编号或“未定义”以在提及下拉语法中使用,但我想显示用户的名称而不提及它们。
【问题讨论】:
标签: javascript node.js sqlite