added username system

This commit is contained in:
Xeovalyte 2023-08-05 11:16:18 +02:00
parent e5644462b9
commit 1e72ecdf53
8 changed files with 87 additions and 5 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

Binary file not shown.

View File

@ -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,

View File

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