Move user user existance checking to databaseHelperFunctions file

This commit is contained in:
baz 2023-12-18 22:49:33 +00:00
parent 7d4ca51aa4
commit 91bffb65c7
2 changed files with 31 additions and 11 deletions

View File

@ -1,5 +1,5 @@
const { SlashCommandBuilder } = require('discord.js');
const { Users } = require ('../../dbObjects.js');
const { checkUserRegistration } = require('../../databaseHelperFunctions.js');
module.exports = {
data: new SlashCommandBuilder()
@ -10,16 +10,8 @@ module.exports = {
// interaction.user is the object representing the user who ran the command
// interaction.member is the GuildMember object, which represents the user in the specific guild
const user = await Users.findOne({ where: { discord_id: interaction.user.id } })
.catch((err) => {
console.log(err);
});
if (user) return interaction.reply(`User "${interaction.user.username}" is already registered`);
if (checkUserRegistration(interaction.user)) return interaction.reply(`User "${interaction.user.username}" is registered`);
await Users.create({ discord_id: interaction.user.id, username: interaction.user.username })
.then(await interaction.reply(`${interaction.user.username} was manually registered.`))
.catch((err) => {
console.log(err);
});
return interaction.reply(`Issue checking registration with "${interaction.user.username}".`);
},
};

View File

@ -0,0 +1,28 @@
const { SlashCommandBuilder } = require('discord.js');
const { Users } = require ('./dbObjects.js');
async function checkUserRegistration(user) {
let u = await Users.findOne({ where: { discord_id: user.id } })
.catch((err) => {
console.log(err);
});
if (u) return true;
await Users.create({ discord_id: user.id, username: user.username })
.then((data) => {
u = data;
})
.catch((err) => {
console.log(err);
});
if (u) return true;
return false;
}
module.exports = {
checkUserRegistration,
};