Polarcraft/discordbot/functions/utils.js

30 lines
774 B
JavaScript
Raw Normal View History

2023-08-05 11:16:18 +02:00
const applyUsername = async (user, member) => {
2023-08-11 11:14:59 +02:00
await user.reload();
2023-08-05 11:16:18 +02:00
let rawUsername = member.user.globalName;
2023-08-11 11:14:59 +02:00
if (user.useMinecraftUsername && user.minecraftUuid) {
const response = await fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${user.minecraftUuid}`);
2023-08-05 11:16:18 +02:00
const minecraftProfile = await response.json();
rawUsername = minecraftProfile.name;
}
2023-08-05 12:21:30 +02:00
user.rawUsername = rawUsername;
2023-08-11 11:14:59 +02:00
const username = await getUsername(user);
2023-08-05 12:21:30 +02:00
await user.save();
2023-08-11 11:14:59 +02:00
await member.setNickname(username.slice(0, 32));
2023-08-05 11:16:18 +02:00
2023-08-11 11:14:59 +02:00
return username;
2023-08-05 11:16:18 +02:00
};
2023-08-11 11:14:59 +02:00
const getUsername = async (user) => {
const team = await user.getTeam();
return team ? user.rawUsername + ' [' + team.name + ']' : user.rawUsername;
2023-08-05 12:21:30 +02:00
};
module.exports = { applyUsername, getUsername };