diff --git a/discordbot/commands/adduser.js b/discordbot/commands/adduser.js new file mode 100644 index 0000000..1abf2d1 --- /dev/null +++ b/discordbot/commands/adduser.js @@ -0,0 +1,28 @@ +const { SlashCommandBuilder } = require('discord.js'); +const { simpleEmbed } = require('../functions/embeds.js'); +const { Minecraft, Users } = require('../functions/models.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('adduser') + .setDescription('Add a user to the system') + .addUserOption(option => option + .setName('user') + .setDescription('The user to add')), + + async execute(interaction) { + const userId = interaction.options.getUser('user') ? interaction.options.getUser('user').id : interaction.user.id; + + try { + await Users.create({ + id: userId, + }); + + await interaction.reply({ embeds: [simpleEmbed('Added user to the system')], ephemeral: true }); + } catch (error) { + console.error(error); + + await interaction.reply({ embeds: [simpleEmbed('There was an error while adding the user')], ephemeral: true }); + } + }, +}; diff --git a/discordbot/commands/removewhitelist.js b/discordbot/commands/removewhitelist.js new file mode 100644 index 0000000..682c686 --- /dev/null +++ b/discordbot/commands/removewhitelist.js @@ -0,0 +1,28 @@ +const { SlashCommandBuilder } = require('discord.js'); +const { simpleEmbed } = require('../functions/embeds.js'); +const { Minecraft, Users } = require('../functions/models.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('removewhitelist') + .setDescription('Remove yourself from the whitelist'), + + async execute(interaction) { + try { + const user = await Users.findOne({ where: { id: interaction.user.id } }); + + if (!user.minecraftUUID) return await interaction.reply({ embeds: [simpleEmbed('You are not whitelisted')], ephemeral: true }); + + await Minecraft.destroy({ where: { uuid: user.minecraftUUID } }); + user.minecraftUUID = ''; + + await user.save(); + + await interaction.reply({ embeds: [simpleEmbed('Successfully removed you from the whitelist')], ephemeral: true }); + } catch (error) { + console.error(error); + + await interaction.reply({ embeds: [simpleEmbed('There was an error while removing user from the whitelist')], ephemeral: true }); + } + }, +}; diff --git a/discordbot/commands/whitelist.js b/discordbot/commands/whitelist.js new file mode 100644 index 0000000..5394966 --- /dev/null +++ b/discordbot/commands/whitelist.js @@ -0,0 +1,41 @@ +const { SlashCommandBuilder } = require('discord.js'); +const { simpleEmbed } = require('../functions/embeds.js'); +const { Minecraft, Users } = require('../functions/models.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('whitelist') + .setDescription('Whitelist yourself on the Minecraft server') + .addStringOption(option => option + .setName('code') + .setDescription('6 digit code from Minecraft') + .setRequired(true)), + + async execute(interaction) { + const code = interaction.options.getString('code'); + + if (code.length !== 6) return await interaction.reply({ embeds: [simpleEmbed('The code must be 6 digits long')], ephemeral: true }); + + try { + const minecraftCol = await Minecraft.findOne({ where: { code: code } }); + + if (!minecraftCol) return await interaction.reply({ embeds: [simpleEmbed('The code was not linked with a Minecraft account')], ephemeral: true }); + + if (minecraftCol.whitelisted) return await interaction.reply({ embeds: [simpleEmbed('Minecraft account already whitelisted')], ephemeral: true }); + + const user = await Users.findOne({ where: { id: interaction.user.id } }); + + minecraftCol.whitelisted = true; + minecraftCol.code = ''; + + user.minecraftUUID = minecraftCol.uuid; + + await minecraftCol.save(); + await user.save(); + } catch (error) { + console.error(error); + } + + await interaction.reply({ embeds: [simpleEmbed('Successfully whitelisted player ...')], ephemeral: true }); + }, +}; diff --git a/discordbot/database.sqlite b/discordbot/database.sqlite index 6bfa134..20a4ae2 100644 Binary files a/discordbot/database.sqlite and b/discordbot/database.sqlite differ diff --git a/discordbot/functions/models.js b/discordbot/functions/models.js index e8eccc3..820f8fa 100644 --- a/discordbot/functions/models.js +++ b/discordbot/functions/models.js @@ -10,6 +10,9 @@ const Users = sequelize.define('users', { teamId: { type: Sequelize.STRING, }, + minecraftUUID: { + type: Sequelize.UUID, + }, moderator: { type: Sequelize.BOOLEAN, defaultValue: false, diff --git a/discordbot/routes/verifyminecraft.js b/discordbot/routes/verifyminecraft.js index c204a00..da9c863 100644 --- a/discordbot/routes/verifyminecraft.js +++ b/discordbot/routes/verifyminecraft.js @@ -16,7 +16,7 @@ router.post('/', async (req, res) => { }, }); - res.send({ code: minecraftCol[0].code }); + res.send({ code: minecraftCol[0].code, whitelisted: minecraftCol[0].whitelisted, username: 'Username', rawUsername: 'RawUsername' }); } catch (error) { console.log(error);