From e4c569721ed68f6c3c043ead71a15df641079a1d Mon Sep 17 00:00:00 2001 From: sebm253 <42180891+sebm253@users.noreply.github.com> Date: Sun, 1 Dec 2024 23:22:03 +0100 Subject: [PATCH] Introduce ItemsResponse --- rest/applications.go | 14 +++----------- rest/items_response.go | 17 +++++++++++++++++ rest/soundboard_sounds.go | 14 +++----------- 3 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 rest/items_response.go diff --git a/rest/applications.go b/rest/applications.go index 906e5c5f..36295413 100644 --- a/rest/applications.go +++ b/rest/applications.go @@ -42,7 +42,7 @@ type Applications interface { DeleteTestEntitlement(applicationID snowflake.ID, entitlementID snowflake.ID, opts ...RequestOpt) error ConsumeEntitlement(applicationID snowflake.ID, entitlementID snowflake.ID, opts ...RequestOpt) error - GetApplicationEmojis(applicationID snowflake.ID, opts ...RequestOpt) ([]discord.Emoji, error) + GetApplicationEmojis(applicationID snowflake.ID, opts ...RequestOpt) (ItemsResponse[discord.Emoji], error) GetApplicationEmoji(applicationID snowflake.ID, emojiID snowflake.ID, opts ...RequestOpt) (*discord.Emoji, error) CreateApplicationEmoji(applicationID snowflake.ID, emojiCreate discord.EmojiCreate, opts ...RequestOpt) (*discord.Emoji, error) UpdateApplicationEmoji(applicationID snowflake.ID, emojiID snowflake.ID, emojiUpdate discord.EmojiUpdate, opts ...RequestOpt) (*discord.Emoji, error) @@ -220,12 +220,8 @@ func (s *applicationsImpl) ConsumeEntitlement(applicationID snowflake.ID, entitl return s.client.Do(ConsumeEntitlement.Compile(nil, applicationID, entitlementID), nil, nil, opts...) } -func (s *applicationsImpl) GetApplicationEmojis(applicationID snowflake.ID, opts ...RequestOpt) (emojis []discord.Emoji, err error) { - var rs emojisResponse - err = s.client.Do(GetApplicationEmojis.Compile(nil, applicationID), nil, &rs, opts...) - if err == nil { - emojis = rs.Items - } +func (s *applicationsImpl) GetApplicationEmojis(applicationID snowflake.ID, opts ...RequestOpt) (emojis ItemsResponse[discord.Emoji], err error) { + err = s.client.Do(GetApplicationEmojis.Compile(nil, applicationID), nil, &emojis, opts...) return } @@ -260,7 +256,3 @@ func unmarshalApplicationCommandsToApplicationCommands(unmarshalCommands []disco } return commands } - -type emojisResponse struct { - Items []discord.Emoji `json:"items"` -} diff --git a/rest/items_response.go b/rest/items_response.go new file mode 100644 index 00000000..ffff9c6f --- /dev/null +++ b/rest/items_response.go @@ -0,0 +1,17 @@ +package rest + +import "github.com/disgoorg/json" + +// ItemsResponse serves as a utility to flatten objects wrapped in an "items" object into a slice of objects. +type ItemsResponse[T any] []T + +func (rs *ItemsResponse[T]) UnmarshalJSON(data []byte) error { + var response struct { + Items []T `json:"items"` + } + if err := json.Unmarshal(data, &response); err != nil { + return err + } + *rs = response.Items + return nil +} diff --git a/rest/soundboard_sounds.go b/rest/soundboard_sounds.go index 7886e6b6..49fdac45 100644 --- a/rest/soundboard_sounds.go +++ b/rest/soundboard_sounds.go @@ -13,7 +13,7 @@ func NewSoundboardSounds(client Client) SoundboardSounds { type SoundboardSounds interface { GetSoundboardDefaultSounds(opts ...RequestOpt) ([]discord.SoundboardSound, error) - GetGuildSoundboardSounds(guildID snowflake.ID, opts ...RequestOpt) ([]discord.SoundboardSound, error) + GetGuildSoundboardSounds(guildID snowflake.ID, opts ...RequestOpt) (ItemsResponse[discord.SoundboardSound], error) CreateGuildSoundboardSound(guildID snowflake.ID, soundCreate discord.SoundboardSoundCreate, opts ...RequestOpt) (*discord.SoundboardSound, error) GetGuildSoundboardSound(guildID snowflake.ID, soundID snowflake.ID, opts ...RequestOpt) (*discord.SoundboardSound, error) UpdateGuildSoundboardSound(guildID snowflake.ID, soundID snowflake.ID, soundUpdate discord.SoundboardSoundUpdate, opts ...RequestOpt) (*discord.SoundboardSound, error) @@ -30,12 +30,8 @@ func (s *soundsImpl) GetSoundboardDefaultSounds(opts ...RequestOpt) (sounds []di return } -func (s *soundsImpl) GetGuildSoundboardSounds(guildID snowflake.ID, opts ...RequestOpt) (sounds []discord.SoundboardSound, err error) { - var rs soundsResponse - err = s.client.Do(GetGuildSoundboardSounds.Compile(nil, guildID), nil, &rs, opts...) - if err == nil { - sounds = rs.Items - } +func (s *soundsImpl) GetGuildSoundboardSounds(guildID snowflake.ID, opts ...RequestOpt) (sounds ItemsResponse[discord.SoundboardSound], err error) { + err = s.client.Do(GetGuildSoundboardSounds.Compile(nil, guildID), nil, &sounds, opts...) return } @@ -61,7 +57,3 @@ func (s *soundsImpl) DeleteGuildSoundboardSound(guildID snowflake.ID, soundID sn func (s *soundsImpl) SendSoundboardSound(channelID snowflake.ID, sendSound discord.SendSoundboardSound, opts ...RequestOpt) error { return s.client.Do(SendSoundboardSound.Compile(nil, channelID), sendSound, nil, opts...) } - -type soundsResponse struct { - Items []discord.SoundboardSound `json:"items"` -}