Skip to content

Commit

Permalink
Introduce ItemsResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
sebm253 committed Dec 1, 2024
1 parent 7266716 commit e4c5697
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
14 changes: 3 additions & 11 deletions rest/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -260,7 +256,3 @@ func unmarshalApplicationCommandsToApplicationCommands(unmarshalCommands []disco
}
return commands
}

type emojisResponse struct {
Items []discord.Emoji `json:"items"`
}
17 changes: 17 additions & 0 deletions rest/items_response.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 3 additions & 11 deletions rest/soundboard_sounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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"`
}

0 comments on commit e4c5697

Please sign in to comment.