Polarcraft/discordbot/commands/whitelist.js

49 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-08-04 19:27:29 +02:00
const { SlashCommandBuilder } = require('discord.js');
const { simpleEmbed } = require('../functions/embeds.js');
const { Minecraft, Users } = require('../functions/models.js');
2023-08-05 11:16:18 +02:00
const { applyUsername } = require('../functions/utils.js');
2023-08-04 19:27:29 +02:00
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 });
2023-08-05 11:16:18 +02:00
const user = await Users.findOne({ where: { id: interaction.user.id } });
if (!user) return await interaction.reply({ embeds: [simpleEmbed('There was an error while finding the user')], ephemeral: true });
if (user.minecraftUUID) return await interaction.reply({ embeds: [simpleEmbed('You are already whitelisted')], ephemeral: true });
2023-08-04 19:27:29 +02:00
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 });
minecraftCol.whitelisted = true;
2023-08-05 11:16:18 +02:00
delete minecraftCol.code;
2023-08-04 19:27:29 +02:00
user.minecraftUUID = minecraftCol.uuid;
await minecraftCol.save();
await user.save();
2023-08-05 11:16:18 +02:00
await applyUsername(user, interaction.member);
await interaction.reply({ embeds: [simpleEmbed('You are successfully whitelisted')], ephemeral: true });
2023-08-04 19:27:29 +02:00
} catch (error) {
console.error(error);
}
},
};