diff --git a/discordbot/commands/adduser.js b/discordbot/commands/adduser.js index 1abf2d1..5242635 100644 --- a/discordbot/commands/adduser.js +++ b/discordbot/commands/adduser.js @@ -1,6 +1,6 @@ const { SlashCommandBuilder } = require('discord.js'); const { simpleEmbed } = require('../functions/embeds.js'); -const { Minecraft, Users } = require('../functions/models.js'); +const { Users } = require('../functions/models.js'); module.exports = { data: new SlashCommandBuilder() diff --git a/discordbot/commands/dennis.js b/discordbot/commands/dennis.js new file mode 100644 index 0000000..e63db53 --- /dev/null +++ b/discordbot/commands/dennis.js @@ -0,0 +1,11 @@ +const { SlashCommandBuilder } = require('discord.js'); +const { simpleEmbed } = require('../functions/embeds.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('dennis') + .setDescription('Dennis.'), + async execute(interaction) { + await interaction.reply({ embeds: [simpleEmbed('Dennis is koel')], ephemeral: true }); + }, +}; diff --git a/discordbot/commands/removewhitelist.js b/discordbot/commands/removewhitelist.js index 682c686..47c4c0f 100644 --- a/discordbot/commands/removewhitelist.js +++ b/discordbot/commands/removewhitelist.js @@ -1,6 +1,7 @@ const { SlashCommandBuilder } = require('discord.js'); const { simpleEmbed } = require('../functions/embeds.js'); const { Minecraft, Users } = require('../functions/models.js'); +const { applyUsername } = require('../functions/utils.js'); module.exports = { data: new SlashCommandBuilder() @@ -18,11 +19,13 @@ module.exports = { await user.save(); + await applyUsername(user, interaction.member); + 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 }); + await interaction.reply({ embeds: [simpleEmbed('There was an error while removing you from the whitelist')], ephemeral: true }); } }, }; diff --git a/discordbot/commands/setusername.js b/discordbot/commands/setusername.js new file mode 100644 index 0000000..c48af97 --- /dev/null +++ b/discordbot/commands/setusername.js @@ -0,0 +1,38 @@ +const { SlashCommandBuilder } = require('discord.js'); +const { simpleEmbed } = require('../functions/embeds.js'); +const { Users } = require('../functions/models.js'); +const { applyUsername } = require('../functions/utils.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('setusername') + .setDescription('Choose between Discord or Minecraft username') + .addStringOption(option => option + .setName('type') + .setDescription('Discord or Minecraft') + .setRequired(true) + .addChoices( + { name: 'Discord', value: 'discord' }, + { name: 'Minecraft', value: 'minecraft' }, + )), + + async execute(interaction) { + const usernameType = interaction.options.getString('type'); + + try { + const user = await Users.findOne({ where: { id: interaction.user.id } }); + + user.useMinecraftUsername = usernameType === 'minecraft' ? true : false; + + await user.save(); + + await applyUsername(user, interaction.member); + + await interaction.reply({ embeds: [simpleEmbed('Successfully changed your username type')], ephemeral: true }); + } catch (error) { + console.error(error); + + await interaction.reply({ embeds: [simpleEmbed('There was an error while changing your username type')], ephemeral: true }); + } + }, +}; diff --git a/discordbot/commands/whitelist.js b/discordbot/commands/whitelist.js index 5394966..78d754f 100644 --- a/discordbot/commands/whitelist.js +++ b/discordbot/commands/whitelist.js @@ -1,6 +1,7 @@ const { SlashCommandBuilder } = require('discord.js'); const { simpleEmbed } = require('../functions/embeds.js'); const { Minecraft, Users } = require('../functions/models.js'); +const { applyUsername } = require('../functions/utils.js'); module.exports = { data: new SlashCommandBuilder() @@ -15,6 +16,10 @@ module.exports = { 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 }); + 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 }); try { const minecraftCol = await Minecraft.findOne({ where: { code: code } }); @@ -23,19 +28,21 @@ module.exports = { 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 = ''; + delete minecraftCol.code; user.minecraftUUID = minecraftCol.uuid; await minecraftCol.save(); await user.save(); + + await applyUsername(user, interaction.member); + + await interaction.reply({ embeds: [simpleEmbed('You are successfully whitelisted')], ephemeral: true }); } 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 20a4ae2..bf6f9a1 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 820f8fa..1b42aa5 100644 --- a/discordbot/functions/models.js +++ b/discordbot/functions/models.js @@ -13,6 +13,13 @@ const Users = sequelize.define('users', { minecraftUUID: { type: Sequelize.UUID, }, + useMinecraftUsername: { + type: Sequelize.BOOLEAN, + defaultValue: false, + }, + rawUsername: { + type: Sequelize.STRING, + }, moderator: { type: Sequelize.BOOLEAN, defaultValue: false, diff --git a/discordbot/functions/utils.js b/discordbot/functions/utils.js new file mode 100644 index 0000000..34beea7 --- /dev/null +++ b/discordbot/functions/utils.js @@ -0,0 +1,16 @@ +const applyUsername = async (user, member) => { + let rawUsername = member.user.globalName; + + if (user.useMinecraftUsername && user.minecraftUUID) { + const response = await fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${user.minecraftUUID}`); + const minecraftProfile = await response.json(); + + rawUsername = minecraftProfile.name; + } + + await member.setNickname(rawUsername); + + return rawUsername; +}; + +module.exports = { applyUsername };