From 1e72ecdf537ff2f382612200b2ad3992f545d0ab Mon Sep 17 00:00:00 2001 From: Xeovalyte Date: Sat, 5 Aug 2023 11:16:18 +0200 Subject: [PATCH] added username system --- discordbot/commands/adduser.js | 2 +- discordbot/commands/dennis.js | 11 +++++++ discordbot/commands/removewhitelist.js | 5 +++- discordbot/commands/setusername.js | 38 +++++++++++++++++++++++++ discordbot/commands/whitelist.js | 13 +++++++-- discordbot/database.sqlite | Bin 36864 -> 36864 bytes discordbot/functions/models.js | 7 +++++ discordbot/functions/utils.js | 16 +++++++++++ 8 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 discordbot/commands/dennis.js create mode 100644 discordbot/commands/setusername.js create mode 100644 discordbot/functions/utils.js 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 20a4ae2945ffa26dbc2cce5fa19153b905c49520..bf6f9a113f310d056b3ea358a8690547d96a1911 100644 GIT binary patch delta 755 zcma)(&1(}u7{+Hm-0WtvI}NBI7E2CdYju~|`PkWAPaz;ikV;_%4?<(Ifi^XVv?4+w zwdlFZTs%p@lOF7${S&+jLN59Tcu-HhmEyr^lYq26bYO;ecxIk?=J#$6)XjnVX1Z7d z09bNP#-+En$kiI~+rJ!m08IN3Km&et_nfZVckkJUPHne1^(U{piSk->?CmRCcXGK> zsieH>tT&#lX3y5L-rDhEiN}SNi?Y1G+|8Q3MteP#TDS2u6W zRu|_mV>iT?!bd&#@{ul9>>nyI#0z&;bX;3A$pu-H-97g~V5)E)z;|$7rkR1ps%oTG zS-fAk-j4}G5yuD<4@Dkg6%;1{O(^oCkW_9Wlpu6kU4=UUeuq2qA0MDT zr=^M*EL^Lqvs!AMC&0*K2;grxly;_^N9{DF-zQkK!%){8#mH-Ag|LV)G2HeFqdWTm z{)PMU+)p@tq>sRV*6BPNF1!n*l^+A%xY^-i5BUkace|hOVzdeCl@vhtGKumJT(_y( delta 676 zcmaJ;OKTKC5bl}Tot?*aR|0`0K1dE?4!T3Xrh8^?8W7^eTvp6UqU?sK*3qYui-LFwfe)X^PJI^7#!KWexjGmh`w@Blh0r|2U(fWP)@qGL!+Ex_ zzIf+B`%$mG);n;4nW*10AL_5GA$g02=6?LbIOK{eV$&xxgs&`j+jrL%JL|ogi0qdL z;1>!?8G{`%&wK~A5Nz3I&mrsvlO4xzh1k#=7q4;3MU!StDyNw)xGH3pC@H5eQCd)X z0@VfOiOj_j8d-h^_7Ln@=Fd=>@mir7+`cez2<#)+N50Le!cX{OQ{KV#SgDGK9Y69W z9g$`tRn;<7BFpl$Qifa$u5zyoQ>vKGUCR*Z)bntN;SF-h5RoCwTfUFv+!H5Ys1*cr zbAqKhr8*NrkIwJ?rQTlA)SpM 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 };