feat: Added set default functions and refresh discord data
Some checks failed
Build and Deploy / Deploy Web (push) Failing after 1s
Build and Deploy / Deploy Discord Bot (push) Failing after 0s

This commit is contained in:
2023-06-15 16:55:23 +02:00
parent c433e25fba
commit 0588dd1c45
4 changed files with 90 additions and 22 deletions

View File

@@ -8,24 +8,6 @@ type AccessTokenResponse = {
scope: string
}
type DiscordUser = {
id: string,
username: string,
discriminator: string,
avatar: string,
bot?: boolean,
system?: boolean,
mfa_enabled?: boolean,
banner?: string,
accent_color?: number,
locale?: string,
verified?: boolean,
email?: string,
flags?: number,
premium_type?: number,
public_flags?: number
}
export default defineEventHandler(async (event) => {
const { code }: { code?: string } = getQuery(event)
@@ -67,7 +49,7 @@ export default defineEventHandler(async (event) => {
$set: {
discord: {
id: authorizationResponse.id,
username: authorizationResponse.username,
username: authorizationResponse.global_name ?? authorizationResponse.username,
avatarHash: authorizationResponse.avatar
}
},

View File

@@ -0,0 +1,25 @@
export default defineEventHandler(async (event) => {
const userId: string = event.context.params ? event.context.params.id : '@me'
const user = await getUser(userId, event)
const auth = getAuth(event)
try {
const authorizationResponse: DiscordUser = await $fetch('https://discord.com/api/users/@me', {
headers: {
authorization: `Bearer ${auth.accessToken}`
}
})
user.discord.username = authorizationResponse.global_name ?? authorizationResponse.username
user.discord.avatarHash = authorizationResponse.avatar
await applyUsername(user)
} catch (e) {
console.error('Failed to update document', e)
throw createError('Failed to update document')
}
return user.discord
})