diff --git a/bot/events/message.js b/bot/events/message.js index e50954e..72b53c4 100644 --- a/bot/events/message.js +++ b/bot/events/message.js @@ -17,6 +17,7 @@ module.exports = class { const data = {}; data.user = await this.client.db.getUser(message.author.id); + if (!data.user) data.user = await this.client.db.createUser(message.author.id); // Prefix cache if (this.client.prefixCache.has(message.author.id)) { @@ -29,7 +30,11 @@ module.exports = class { if (message.guild) { data.guild = await this.client.db.getGuild(message.guild.id); + if (!data.guild) data.guild = await this.client.db.createGuild(message.guild.id); + // data.member = await this.client.db.getMember(message.guild.id, message.author.id); + // if (!data.member) data.member = await this.client.db.createMember(message.guild.id, message.author.id) + if (this.client.prefixCache.has(message.guild.id)) { guildPrefix = this.client.prefixCache.get(message.guild.id); prefixes.push(guildPrefix); diff --git a/bot/util/database.js b/bot/util/database.js index 5685621..cd75afb 100644 --- a/bot/util/database.js +++ b/bot/util/database.js @@ -105,16 +105,35 @@ class Database { async deleteGuild (id) { await this.pool.query('DELETE FROM guilds WHERE guild_id = $1;', [id]); await this.pool.query('DELETE FROM members WHERE member_id LIKE $1;', [`${id}%`]); + return; } async deleteMember (guild_id, user_id) { const key = guild_id + ':' + user_id; await this.pool.query('DELETE FROM members WHERE member_id = $1;', [key]); + return; } async deleteUser (id) { await this.pool.query('DELETE FROM users WHERE user_id = $1;', [id]); await this.pool.query('DELETE FROM members WHERE member_id LIKE $1;', [`${id}%`]); + return; + } + + async createGuild (id) { + const res = await this.pool.query('INSERT INTO guilds (guild_id) VALUES ($1) RETURNING *;', [id]); + return res; + } + + async createMember (guild_id, user_id) { + const key = guild_id + ':' + user_id; + const res = await this.pool.query('INSERT INTO members (member_id) VALUES ($1) RETURNING *;', [key]); + return res.rows[0]; + } + + async createUser (id) { + const res = await this.pool.query('INSERT INTO users (user_id) VALUES ($1) RETURNING *;', [id]); + return res.rows[0]; } }