Polarcraft/web/server/api/discord/refreshusername.js

27 lines
691 B
JavaScript
Raw Normal View History

2023-05-09 15:23:31 +02:00
export default defineEventHandler(async (event) => {
2023-05-11 19:46:18 +02:00
const user = await getAuth(event)
2023-05-09 15:23:31 +02:00
const userResult = await $fetch('https://discord.com/api/users/@me', {
headers: {
2023-05-11 19:46:18 +02:00
authorization: `Bearer ${user.accessToken}`
2023-05-09 15:23:31 +02:00
}
})
const coll = db.collection('users')
const doc = {
discord: {
id: userResult.id,
username: userResult.username,
avatarHash: userResult.avatar || null
},
2023-05-11 19:46:18 +02:00
username: user.useMinecraftUsername ? user.minecraft.username : userResult.username
2023-05-09 15:23:31 +02:00
}
2023-05-11 19:46:18 +02:00
const newUser = await coll.findOneAndUpdate({ 'discord.id': userResult.id }, { $set: doc }, { returnDocument: 'after' })
applyUsername(newUser)
2023-05-09 15:23:31 +02:00
return doc.discord
});