From e5644462b9911a9d59456cc5a2fa28dfd952cfa6 Mon Sep 17 00:00:00 2001 From: Xeovalyte Date: Fri, 4 Aug 2023 19:27:29 +0200 Subject: [PATCH] Added whitelist function --- discordbot/commands/adduser.js | 28 +++++++++++++++++ discordbot/commands/removewhitelist.js | 28 +++++++++++++++++ discordbot/commands/whitelist.js | 41 +++++++++++++++++++++++++ discordbot/database.sqlite | Bin 36864 -> 36864 bytes discordbot/functions/models.js | 3 ++ discordbot/routes/verifyminecraft.js | 2 +- 6 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 discordbot/commands/adduser.js create mode 100644 discordbot/commands/removewhitelist.js create mode 100644 discordbot/commands/whitelist.js 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 6bfa1346c29dd18e734a07851faa75c90add4f03..20a4ae2945ffa26dbc2cce5fa19153b905c49520 100644 GIT binary patch delta 745 zcma)3ziSjh6rSCUd$W5tGfBXl5+v89FuTm}+1=9EY?o$(>2i0u3l^T31O!1Yhoy~S zo0KU?>LwsQ`GOB?%V@c}iJ#(o?*}KLH&_eGC+K@PcwVX0>viYFTG_63 zW*_!C?X6~arzrCK>y5LwJgU8!!f(N-+(KVnQ7s2}5%WjmkJ}Xy+LVCg7l3CRhKG2C z`3~6!WPdm_{gU*1@OIbnivZ<$yU8?TMn$aEY(oq1A$eor#$5N<>$vsb2c3s<#{MTx{o7k7b<3snS=7S9FZ7y3l2yeAjRE&#hybV6nL{XQKB%s8#g delta 798 zcmaKqO=}ZD7{_OKvYT#_ou`GyP=Yi)d1$lD%(Js^;K@_Pmns&_Ed^Uap#hs7dfFDh zfMq{`J&C?TfKsxT;$WTw_3V8ds2>Ta`?-LG}rdxN>zZ`I1d%W}EVXc%wX*~8jq_Nn%P9&}3C ztLj%1?QDfjWL*t;#M0-M-JV%kUF~n~^tXC%_IkS; z+b{HqDnb=VF0sv{HpiBT6O0m$o>W0=ne&A6|Eo_+_EM1?U=+owV3j5qCQ5po^XbrQ z>Jw@&{(%bY3yCsB8N>x^?_m6 zC%{1Zku2t%AWszcko&D=HLZ|;TX3a7w*zs<}f0&Iz1r)*0NYQ{lv+6dVI7!!VSg@B896 Db_uh> 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);