Added whitelist function

This commit is contained in:
Xeovalyte 2023-08-04 19:27:29 +02:00
parent 1b89c4440b
commit e5644462b9
6 changed files with 101 additions and 1 deletions

View File

@ -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 });
}
},
};

View File

@ -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 });
}
},
};

View File

@ -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 });
},
};

Binary file not shown.

View File

@ -10,6 +10,9 @@ const Users = sequelize.define('users', {
teamId: {
type: Sequelize.STRING,
},
minecraftUUID: {
type: Sequelize.UUID,
},
moderator: {
type: Sequelize.BOOLEAN,
defaultValue: false,

View File

@ -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);