Polarcraft/discordbot/routes/verifyminecraft.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-08-04 17:05:49 +02:00
const express = require('express');
2023-08-05 12:21:30 +02:00
const { Minecraft, Users } = require('../functions/models.js');
2023-08-04 17:05:49 +02:00
const router = express.Router();
router.post('/', async (req, res) => {
const { uuid } = req.body;
if (!uuid) return res.status(400).send({ errorMessage: 'UUID is required' });
try {
2023-08-05 12:21:30 +02:00
const user = await Users.findOne({ where: { minecraftUUID: uuid } });
2023-08-11 11:14:59 +02:00
if (!user) {
const minecraftCol = await Minecraft.findOrCreate({
where: { uuid },
defaults: {
code: generateCode().toString(),
},
});
return res.send({ code: minecraftCol[0].code, whitelisted: minecraftCol[0].whitelisted });
}
const team = await user.getTeam();
let username;
if (!team) {
username = user.rawUsername;
} else {
username = '<gray>[</gray>' + `<color:${team.color}>${team.name}</color>` + '<gray>] </gray>' + user.rawUsername;
}
res.send({ whitelisted: true, username, rawUsername: user.rawUsername });
2023-08-04 17:05:49 +02:00
} catch (error) {
console.log(error);
res.status(500).send({ errorMessage: 'Error while verifing minecraft uuid' });
}
});
const generateCode = () => {
return Math.floor(100000 + Math.random() * 900000);
};
module.exports = router;