From 91bffb65c7285f2b8e5dc74a58eab5a0e03296b6 Mon Sep 17 00:00:00 2001 From: baz Date: Mon, 18 Dec 2023 22:49:33 +0000 Subject: [PATCH] Move user user existance checking to databaseHelperFunctions file --- commands/testing/register.js | 14 +++----------- databaseHelperFunctions.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 databaseHelperFunctions.js diff --git a/commands/testing/register.js b/commands/testing/register.js index d63a184..5501eb9 100644 --- a/commands/testing/register.js +++ b/commands/testing/register.js @@ -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}".`); }, }; \ No newline at end of file diff --git a/databaseHelperFunctions.js b/databaseHelperFunctions.js new file mode 100644 index 0000000..a1dff62 --- /dev/null +++ b/databaseHelperFunctions.js @@ -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, +}; \ No newline at end of file