discord.js эрорит при обращении к таблице [type-error]
Ошибка:
return msg.reply(`You currently have ${score.points} points and are level ${score.level}!`);
^
TypeError: Cannot read property 'points' of undefined
Я недавно начал изучать js и sqll3 поэтому не бейте палками за плохой код :D
код:
const Discord = require('discord.js');
const bot = new Discord.Client();
const SQLite = require("better-sqlite3");
const sql = new SQLite('./scores.sqlite');
const config = require('./config.json');
let token = config.token
let prefix = config.prefix
let score;
bot.on("ready", () => {
console.log("Bot started!");
});
bot.on("ready", () => {
// Check if the table "points" exists.
const table = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';").get();
if (!table['count(*)']) {
// If the table isn't there, create it and setup the database correctly.
sql.prepare("CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER, money INTEGER);").run();
// Ensure that the "id" row is always unique and indexed.
sql.prepare("CREATE UNIQUE INDEX idx_scores_id ON scores (id);").run();
sql.pragma("synchronous = 1");
sql.pragma("journal_mode = wal");
}
// And then we have two prepared statements to get and set the score data.
bot.getScore = sql.prepare("SELECT * FROM scores WHERE user = ? AND guild = ?");
bot.setScore = sql.prepare("INSERT OR REPLACE INTO scores (id, user, guild, points, level, money) VALUES (@id, @user, @guild, @points, @level, @money);");
});
bot.on("message", message => {
if (message.author.bot) return;
if (message.content.startsWith(prefix)) return;
if (message.guild) {
score = bot.getScore.get(message.author.id, message.guild.id);
if (!score) {
score = { id: `${message.guild.id}-${message.author.id}`, user: message.author.id, guild: message.guild.id, points: 0, level: 1, money:100 }
}
score.points = score.points + 9;
const curLevel = Math.floor(0.1 * Math.sqrt(score.points));
if (score.level < curLevel) {
score.level++;
message.reply(`You've leveled up to level **${curLevel}**! Ain't that dandy?`);
}
bot.setScore.run(score);
}
if (message.content.indexOf(config.prefix) !== 0) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
});
bot.on('message', msg => { //shows ur exp and lvl
if (msg.content === prefix + 'points') {
return msg.reply(`You currently have ${score.points} points and are level ${score.level}!`);
}
});
bot.on('message', msg => { //shows ur money
if (msg.content === prefix + ('money') || msg.content === prefix + ('bux')) {
return msg.reply(`You currently have ${score.money} bux!`);
}
});
bot.login(token);