feat: Added set default functions and refresh discord data
This commit is contained in:
parent
c433e25fba
commit
0588dd1c45
@ -32,10 +32,10 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="space-x-3">
|
||||
<UButton v-if="user.usernameType === 'minecraft'" :disabled="disableButtons">
|
||||
<UButton v-if="user.usernameType === 'minecraft'" :disabled="disableButtons" @click="setDefaultUsername('discord')">
|
||||
Set Default
|
||||
</UButton>
|
||||
<UButton variant="soft" :disabled="disableButtons">
|
||||
<UButton variant="soft" :disabled="disableButtons" @click="refreshDiscordData">
|
||||
Refresh Data
|
||||
</UButton>
|
||||
</div>
|
||||
@ -57,7 +57,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="space-x-3">
|
||||
<UButton v-if="user.usernameType === 'discord'" :disabled="disableButtons">
|
||||
<UButton v-if="user.usernameType === 'discord'" :disabled="disableButtons" @click="setDefaultUsername('minecraft')">
|
||||
Set Default
|
||||
</UButton>
|
||||
<UButton variant="soft" :disabled="disableButtons" @click="refreshMinecraftData">
|
||||
@ -129,4 +129,46 @@ const refreshMinecraftData = async () => {
|
||||
|
||||
disableButtons.value = false
|
||||
}
|
||||
|
||||
const refreshDiscordData = async () => {
|
||||
disableButtons.value = true
|
||||
|
||||
try {
|
||||
const response: any = await $fetch('/api/users/@me/discord')
|
||||
|
||||
user.value.discord = response
|
||||
|
||||
useToast().add({ title: 'Successfully refreshed Discord data', color: 'green' })
|
||||
} catch (e: any) {
|
||||
console.error(e)
|
||||
|
||||
useToast().add({ title: e.statusMessage, color: 'red' })
|
||||
}
|
||||
|
||||
disableButtons.value = false
|
||||
}
|
||||
|
||||
const setDefaultUsername = async (usernameType: string) => {
|
||||
disableButtons.value = true
|
||||
|
||||
try {
|
||||
const response = await $fetch('/api/users/@me/username', {
|
||||
method: 'PUT',
|
||||
body: {
|
||||
usernameType
|
||||
}
|
||||
})
|
||||
|
||||
user.value.usernameType = usernameType
|
||||
user.value.username = response
|
||||
|
||||
useToast().add({ title: 'Successfully updated username', color: 'green' })
|
||||
} catch (e: any) {
|
||||
console.error(e)
|
||||
|
||||
useToast().add({ title: e.statusMessage, color: 'red' })
|
||||
}
|
||||
|
||||
disableButtons.value = false
|
||||
}
|
||||
</script>
|
||||
|
@ -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
|
||||
}
|
||||
},
|
||||
|
25
webv2/server/api/users/[id]/discord.get.ts
Normal file
25
webv2/server/api/users/[id]/discord.get.ts
Normal 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
|
||||
})
|
19
webv2/types/global.d.ts
vendored
19
webv2/types/global.d.ts
vendored
@ -40,4 +40,23 @@ declare global {
|
||||
textChannelId: string,
|
||||
voiceChannelId: string
|
||||
}
|
||||
|
||||
type DiscordUser = {
|
||||
id: string,
|
||||
username: string,
|
||||
discriminator: string,
|
||||
global_name: 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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user