From 13a6bf10ccac5c7f82335bab7731f85bed45d832 Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 8 Jun 2023 02:56:07 +0200 Subject: [PATCH] discord: Support the new username system (#393) * add support for the new username system * rename User.GlobalName to DisplayName --- discord/user.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/discord/user.go b/discord/user.go index f2078c5..3d19216 100644 --- a/discord/user.go +++ b/discord/user.go @@ -8,8 +8,9 @@ import ( type User struct { ID UserID `json:"id"` Username string `json:"username"` - Discriminator string `json:"discriminator"` + Discriminator string `json:"discriminator"` // This is "0" if the user has migrated to the new username system. Avatar Hash `json:"avatar"` + DisplayName string `json:"global_name"` // These fields may be omitted @@ -42,6 +43,10 @@ func (u User) Mention() string { // Tag returns a tag of the user. func (u User) Tag() string { + if u.Discriminator == "0" { + return u.Username + } + return u.Username + "#" + u.Discriminator } @@ -62,11 +67,16 @@ func (u User) AvatarURLWithType(t ImageType) string { return "" } - disc, err := strconv.Atoi(u.Discriminator) - if err != nil { // this should never happen - return "" + var picNo string + if u.Discriminator != "0" { + disc, err := strconv.Atoi(u.Discriminator) + if err != nil { // this should never happen + return "" + } + picNo = strconv.Itoa(disc % 5) + } else { + picNo = strconv.FormatUint(uint64(u.ID>>22)%5, 10) } - picNo := strconv.Itoa(disc % 5) return "https://cdn.discordapp.com/embed/avatars/" + picNo + ".png" }