Skip to content

Commit

Permalink
Avatar IDs aren't ints
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Feb 18, 2024
1 parent 5f42e6f commit 2a67074
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
21 changes: 13 additions & 8 deletions directmedia.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -232,11 +233,15 @@ func (dma *DirectMediaAPI) AvatarMXC(guildID, userID, avatarID string) (mxc id.C
return
}
animated := strings.HasPrefix(avatarID, "a_")
avatarIDInt, err := strconv.ParseUint(strings.TrimPrefix(avatarID, "a_"), 10, 64)
avatarIDBytes, err := hex.DecodeString(strings.TrimPrefix(avatarID, "a_"))
if err != nil {
dma.log.Warn().Str("avatar_id", avatarID).Msg("Got non-integer avatar ID")
dma.log.Warn().Str("avatar_id", avatarID).Msg("Got non-hex avatar ID")
return
} else if len(avatarIDBytes) != 16 {
dma.log.Warn().Str("avatar_id", avatarID).Msg("Got invalid avatar ID length")
return
}
avatarIDArray := [16]byte(avatarIDBytes)
userIDInt, err := strconv.ParseUint(userID, 10, 64)
if err != nil {
dma.log.Warn().Str("user_id", userID).Msg("Got non-integer user ID")
Expand All @@ -251,13 +256,13 @@ func (dma *DirectMediaAPI) AvatarMXC(guildID, userID, avatarID string) (mxc id.C
return dma.makeMXC(&GuildMemberAvatarMediaData{
GuildID: guildIDInt,
UserID: userIDInt,
AvatarID: avatarIDInt,
AvatarID: avatarIDArray,
Animated: animated,
})
} else {
return dma.makeMXC(&UserAvatarMediaData{
UserID: userIDInt,
AvatarID: avatarIDInt,
AvatarID: avatarIDArray,
Animated: animated,
})
}
Expand Down Expand Up @@ -379,26 +384,26 @@ func (dma *DirectMediaAPI) GetMediaURL(ctx context.Context, encodedMediaID strin
if mediaData.Animated {
url = discordgo.EndpointUserAvatarAnimated(
strconv.FormatUint(mediaData.UserID, 10),
fmt.Sprintf("a_%d", mediaData.AvatarID),
fmt.Sprintf("a_%x", mediaData.AvatarID),
)
} else {
url = discordgo.EndpointUserAvatar(
strconv.FormatUint(mediaData.UserID, 10),
strconv.FormatUint(mediaData.AvatarID, 10),
fmt.Sprintf("%x", mediaData.AvatarID),
)
}
case *GuildMemberAvatarMediaData:
if mediaData.Animated {
url = discordgo.EndpointGuildMemberAvatarAnimated(
strconv.FormatUint(mediaData.GuildID, 10),
strconv.FormatUint(mediaData.UserID, 10),
fmt.Sprintf("a_%d", mediaData.AvatarID),
fmt.Sprintf("a_%x", mediaData.AvatarID),
)
} else {
url = discordgo.EndpointGuildMemberAvatar(
strconv.FormatUint(mediaData.GuildID, 10),
strconv.FormatUint(mediaData.UserID, 10),
strconv.FormatUint(mediaData.AvatarID, 10),
fmt.Sprintf("%x", mediaData.AvatarID),
)
}
default:
Expand Down
4 changes: 2 additions & 2 deletions directmedia_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ func (emd *EmojiMediaData) Wrap() *MediaID {

type UserAvatarMediaData struct {
UserID uint64
AvatarID uint64
Animated bool
AvatarID [16]byte
}

func (uamd *UserAvatarMediaData) Write(to io.Writer) {
Expand All @@ -262,8 +262,8 @@ func (uamd *UserAvatarMediaData) Wrap() *MediaID {
type GuildMemberAvatarMediaData struct {
GuildID uint64
UserID uint64
AvatarID uint64
Animated bool
AvatarID [16]byte
}

func (guamd *GuildMemberAvatarMediaData) Write(to io.Writer) {
Expand Down

0 comments on commit 2a67074

Please sign in to comment.