Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit 576ecf0

Browse files
authored
REST methods cleanup (bwmarrin#1217)
* refactor(GuildRoleEdit)!: use struct params Refactor GuildRoleEdit to accept parameters using a Params struct. Therefore also allow partial edits. * feat(GuildRoleCreate): initial parameters Make GuildRoleCreate accept a struct with initial parameters for a role * refactor(ChannelEdit)!: replace ChannelEditComplex Move funcitonality of ChannelEditComplex to ChannelEdit * chore: deprecate ChannelEditComplex * feat(GuildEmbed)!: allow partial edits * Make fields omitempty * Make `Enabled` field a pointer * refactor(GuildEmbedEdit)!: use params struct Refactor GuildEmbedEdit to accept parameters through GuildEmbed struct * refactor(GuildMemberEdit)!: replace GuildMemberEditComplex Move functionality fo GuildMemberEditComplex to GuildMemberEdit * chore: deprecate GuildMemberEditComplex * refactor(GuildEmojiCreate)!: use struct params Refactor GuildEmojiCreate to take parameters using EmojiParams struct. * refactor(GuildEmojiEdit)!: use struct params Refactor GuildEmojiEdit to take parameters using EmojiParams struct. * refactor(GuildMemberAdd)!: use struct params Refactor GuildMemberAdd to take parameters using GuildMemberAddParams struct. * refactor(GuildTemplateEdit)!: use struct params Refactor GuildTemplateEdit to take parameters using GuildTemplateParams struct. * refactor(GuildTemplateCreate)!: use struct params Refactor GuildTemplateCreate to take parameters using GuildTemplateParams struct. * chore(rest)!: use pointers for Params structs Refactor methods with Params struct to use pointers * chore: grammar and style fixes in comments
1 parent eee9bcb commit 576ecf0

File tree

2 files changed

+103
-116
lines changed

2 files changed

+103
-116
lines changed

restapi.go

Lines changed: 53 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -772,22 +772,10 @@ func (s *Session) GuildMember(guildID, userID string) (st *Member, err error) {
772772
}
773773

774774
// GuildMemberAdd force joins a user to the guild.
775-
// accessToken : Valid access_token for the user.
776775
// guildID : The ID of a Guild.
777776
// userID : The ID of a User.
778-
// nick : Value to set users nickname to
779-
// roles : A list of role ID's to set on the member.
780-
// mute : If the user is muted.
781-
// deaf : If the user is deafened.
782-
func (s *Session) GuildMemberAdd(accessToken, guildID, userID, nick string, roles []string, mute, deaf bool) (err error) {
783-
784-
data := struct {
785-
AccessToken string `json:"access_token"`
786-
Nick string `json:"nick,omitempty"`
787-
Roles []string `json:"roles,omitempty"`
788-
Mute bool `json:"mute,omitempty"`
789-
Deaf bool `json:"deaf,omitempty"`
790-
}{accessToken, nick, roles, mute, deaf}
777+
// data : Parameters of the user to add.
778+
func (s *Session) GuildMemberAdd(guildID, userID string, data *GuildMemberAddParams) (err error) {
791779

792780
_, err = s.RequestWithBucketID("PUT", EndpointGuildMember(guildID, userID), data, EndpointGuildMember(guildID, ""))
793781
if err != nil {
@@ -820,25 +808,11 @@ func (s *Session) GuildMemberDeleteWithReason(guildID, userID, reason string) (e
820808
return
821809
}
822810

823-
// GuildMemberEdit edits the roles of a member.
824-
// guildID : The ID of a Guild.
825-
// userID : The ID of a User.
826-
// roles : A list of role ID's to set on the member.
827-
func (s *Session) GuildMemberEdit(guildID, userID string, roles []string) (err error) {
828-
829-
data := struct {
830-
Roles []string `json:"roles"`
831-
}{roles}
832-
833-
_, err = s.RequestWithBucketID("PATCH", EndpointGuildMember(guildID, userID), data, EndpointGuildMember(guildID, ""))
834-
return
835-
}
836-
837-
// GuildMemberEditComplex edits the nickname and roles of a member.
811+
// GuildMemberEdit edits and returns updated member.
838812
// guildID : The ID of a Guild.
839813
// userID : The ID of a User.
840-
// data : A GuildMemberEditData struct with the new nickname and roles
841-
func (s *Session) GuildMemberEditComplex(guildID, userID string, data GuildMemberParams) (st *Member, err error) {
814+
// data : Updated GuildMember data.
815+
func (s *Session) GuildMemberEdit(guildID, userID string, data *GuildMemberParams) (st *Member, err error) {
842816
var body []byte
843817
body, err = s.RequestWithBucketID("PATCH", EndpointGuildMember(guildID, userID), data, EndpointGuildMember(guildID, ""))
844818
if err != nil {
@@ -849,6 +823,15 @@ func (s *Session) GuildMemberEditComplex(guildID, userID string, data GuildMembe
849823
return
850824
}
851825

826+
// GuildMemberEditComplex edits the nickname and roles of a member.
827+
// NOTE: deprecated, use GuildMemberEdit instead.
828+
// guildID : The ID of a Guild.
829+
// userID : The ID of a User.
830+
// data : A GuildMemberEditData struct with the new nickname and roles
831+
func (s *Session) GuildMemberEditComplex(guildID, userID string, data *GuildMemberParams) (st *Member, err error) {
832+
return s.GuildMemberEdit(guildID, userID, data)
833+
}
834+
852835
// GuildMemberMove moves a guild member from one voice channel to another/none
853836
// guildID : The ID of a Guild.
854837
// userID : The ID of a User.
@@ -1043,11 +1026,11 @@ func (s *Session) GuildRoles(guildID string) (st []*Role, err error) {
10431026
return // TODO return pointer
10441027
}
10451028

1046-
// GuildRoleCreate returns a new Guild Role.
1047-
// guildID: The ID of a Guild.
1048-
func (s *Session) GuildRoleCreate(guildID string) (st *Role, err error) {
1049-
1050-
body, err := s.RequestWithBucketID("POST", EndpointGuildRoles(guildID), nil, EndpointGuildRoles(guildID))
1029+
// GuildRoleCreate creates a new Guild Role and returns it.
1030+
// guildID : The ID of a Guild.
1031+
// data : New Role parameters.
1032+
func (s *Session) GuildRoleCreate(guildID string, data *RoleParams) (st *Role, err error) {
1033+
body, err := s.RequestWithBucketID("POST", EndpointGuildRoles(guildID), data, EndpointGuildRoles(guildID))
10511034
if err != nil {
10521035
return
10531036
}
@@ -1057,30 +1040,17 @@ func (s *Session) GuildRoleCreate(guildID string) (st *Role, err error) {
10571040
return
10581041
}
10591042

1060-
// GuildRoleEdit updates an existing Guild Role with new values
1043+
// GuildRoleEdit updates an existing Guild Role and returns updated Role data.
10611044
// guildID : The ID of a Guild.
10621045
// roleID : The ID of a Role.
1063-
// name : The name of the Role.
1064-
// color : The color of the role (decimal, not hex).
1065-
// hoist : Whether to display the role's users separately.
1066-
// perm : The permissions for the role.
1067-
// mention : Whether this role is mentionable
1068-
func (s *Session) GuildRoleEdit(guildID, roleID, name string, color int, hoist bool, perm int64, mention bool) (st *Role, err error) {
1046+
// data : Updated Role data.
1047+
func (s *Session) GuildRoleEdit(guildID, roleID string, data *RoleParams) (st *Role, err error) {
10691048

10701049
// Prevent sending a color int that is too big.
1071-
if color > 0xFFFFFF {
1072-
err = fmt.Errorf("color value cannot be larger than 0xFFFFFF")
1073-
return nil, err
1050+
if data.Color != nil && *data.Color > 0xFFFFFF {
1051+
return nil, fmt.Errorf("color value cannot be larger than 0xFFFFFF")
10741052
}
10751053

1076-
data := struct {
1077-
Name string `json:"name"` // The role's name (overwrites existing)
1078-
Color int `json:"color"` // The color the role should have (as a decimal, not hex)
1079-
Hoist bool `json:"hoist"` // Whether to display the role's users separately
1080-
Permissions int64 `json:"permissions,string"` // The overall permissions number of the role (overwrites existing)
1081-
Mentionable bool `json:"mentionable"` // Whether this role is mentionable
1082-
}{name, color, hoist, perm, mention}
1083-
10841054
body, err := s.RequestWithBucketID("PATCH", EndpointGuildRole(guildID, roleID), data, EndpointGuildRole(guildID, ""))
10851055
if err != nil {
10861056
return
@@ -1298,12 +1268,10 @@ func (s *Session) GuildEmbed(guildID string) (st *GuildEmbed, err error) {
12981268
return
12991269
}
13001270

1301-
// GuildEmbedEdit returns the embed for a Guild.
1271+
// GuildEmbedEdit edits the embed of a Guild.
13021272
// guildID : The ID of a Guild.
1303-
func (s *Session) GuildEmbedEdit(guildID string, enabled bool, channelID string) (err error) {
1304-
1305-
data := GuildEmbed{enabled, channelID}
1306-
1273+
// data : New GuildEmbed data.
1274+
func (s *Session) GuildEmbedEdit(guildID string, data *GuildEmbed) (err error) {
13071275
_, err = s.RequestWithBucketID("PATCH", EndpointGuildEmbed(guildID), data, EndpointGuildEmbed(guildID))
13081276
return
13091277
}
@@ -1371,19 +1339,10 @@ func (s *Session) GuildEmoji(guildID, emojiID string) (emoji *Emoji, err error)
13711339
return
13721340
}
13731341

1374-
// GuildEmojiCreate creates a new emoji
1342+
// GuildEmojiCreate creates a new Emoji.
13751343
// guildID : The ID of a Guild.
1376-
// name : The Name of the Emoji.
1377-
// image : The base64 encoded emoji image, has to be smaller than 256KB.
1378-
// roles : The roles for which this emoji will be whitelisted, can be nil.
1379-
func (s *Session) GuildEmojiCreate(guildID, name, image string, roles []string) (emoji *Emoji, err error) {
1380-
1381-
data := struct {
1382-
Name string `json:"name"`
1383-
Image string `json:"image"`
1384-
Roles []string `json:"roles,omitempty"`
1385-
}{name, image, roles}
1386-
1344+
// data : New Emoji data.
1345+
func (s *Session) GuildEmojiCreate(guildID string, data *EmojiParams) (emoji *Emoji, err error) {
13871346
body, err := s.RequestWithBucketID("POST", EndpointGuildEmojis(guildID), data, EndpointGuildEmojis(guildID))
13881347
if err != nil {
13891348
return
@@ -1393,18 +1352,11 @@ func (s *Session) GuildEmojiCreate(guildID, name, image string, roles []string)
13931352
return
13941353
}
13951354

1396-
// GuildEmojiEdit modifies an emoji
1355+
// GuildEmojiEdit modifies and returns updated Emoji.
13971356
// guildID : The ID of a Guild.
13981357
// emojiID : The ID of an Emoji.
1399-
// name : The Name of the Emoji.
1400-
// roles : The roles for which this emoji will be whitelisted, if nil or empty the roles will be reset.
1401-
func (s *Session) GuildEmojiEdit(guildID, emojiID, name string, roles []string) (emoji *Emoji, err error) {
1402-
1403-
data := struct {
1404-
Name string `json:"name"`
1405-
Roles []string `json:"roles"`
1406-
}{name, roles}
1407-
1358+
// data : Updated Emoji data.
1359+
func (s *Session) GuildEmojiEdit(guildID, emojiID string, data *EmojiParams) (emoji *Emoji, err error) {
14081360
body, err := s.RequestWithBucketID("PATCH", EndpointGuildEmoji(guildID, emojiID), data, EndpointGuildEmojis(guildID))
14091361
if err != nil {
14101362
return
@@ -1470,16 +1422,9 @@ func (s *Session) GuildTemplates(guildID string) (st []*GuildTemplate, err error
14701422
}
14711423

14721424
// GuildTemplateCreate creates a template for the guild
1473-
// guildID: The ID of the guild
1474-
// name: The name of the template (1-100 characters)
1475-
// description: The description for the template (0-120 characters)
1476-
func (s *Session) GuildTemplateCreate(guildID, name, description string) (st *GuildTemplate) {
1477-
1478-
data := struct {
1479-
Name string `json:"name"`
1480-
Description string `json:"description"`
1481-
}{name, description}
1482-
1425+
// guildID : The ID of the guild
1426+
// data : Template metadata
1427+
func (s *Session) GuildTemplateCreate(guildID string, data *GuildTemplateParams) (st *GuildTemplate) {
14831428
body, err := s.RequestWithBucketID("POST", EndpointGuildTemplates(guildID), data, EndpointGuildTemplates(guildID))
14841429
if err != nil {
14851430
return
@@ -1499,16 +1444,10 @@ func (s *Session) GuildTemplateSync(guildID, templateCode string) (err error) {
14991444
}
15001445

15011446
// GuildTemplateEdit modifies the template's metadata
1502-
// guildID: The ID of the guild
1503-
// templateCode: The code of the template
1504-
// name: The name of the template (1-100 characters)
1505-
// description: The description for the template (0-120 characters)
1506-
func (s *Session) GuildTemplateEdit(guildID, templateCode, name, description string) (st *GuildTemplate, err error) {
1507-
1508-
data := struct {
1509-
Name string `json:"name,omitempty"`
1510-
Description string `json:"description,omitempty"`
1511-
}{name, description}
1447+
// guildID : The ID of the guild
1448+
// templateCode : The code of the template
1449+
// data : New template metadata
1450+
func (s *Session) GuildTemplateEdit(guildID, templateCode string, data *GuildTemplateParams) (st *GuildTemplate, err error) {
15121451

15131452
body, err := s.RequestWithBucketID("PATCH", EndpointGuildTemplateSync(guildID, templateCode), data, EndpointGuildTemplateSync(guildID, ""))
15141453
if err != nil {
@@ -1544,26 +1483,26 @@ func (s *Session) Channel(channelID string) (st *Channel, err error) {
15441483
return
15451484
}
15461485

1547-
// ChannelEdit edits the given channel
1548-
// channelID : The ID of a Channel
1549-
// name : The new name to assign the channel.
1550-
func (s *Session) ChannelEdit(channelID, name string) (*Channel, error) {
1551-
return s.ChannelEditComplex(channelID, &ChannelEdit{
1552-
Name: name,
1553-
})
1554-
}
1555-
1556-
// ChannelEditComplex edits an existing channel, replacing the parameters entirely with ChannelEdit struct
1557-
// channelID : The ID of a Channel
1558-
// data : The channel struct to send
1559-
func (s *Session) ChannelEditComplex(channelID string, data *ChannelEdit) (st *Channel, err error) {
1486+
// ChannelEdit edits the given channel and returns the updated Channel data.
1487+
// channelID : The ID of a Channel.
1488+
// data : New Channel data.
1489+
func (s *Session) ChannelEdit(channelID string, data *ChannelEdit) (st *Channel, err error) {
15601490
body, err := s.RequestWithBucketID("PATCH", EndpointChannel(channelID), data, EndpointChannel(channelID))
15611491
if err != nil {
15621492
return
15631493
}
15641494

15651495
err = unmarshal(body, &st)
15661496
return
1497+
1498+
}
1499+
1500+
// ChannelEditComplex edits an existing channel, replacing the parameters entirely with ChannelEdit struct
1501+
// NOTE: deprecated, use ChannelEdit instead
1502+
// channelID : The ID of a Channel
1503+
// data : The channel struct to send
1504+
func (s *Session) ChannelEditComplex(channelID string, data *ChannelEdit) (st *Channel, err error) {
1505+
return s.ChannelEdit(channelID, data)
15671506
}
15681507

15691508
// ChannelDelete deletes the given channel

structs.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,17 @@ func (e *Emoji) APIName() string {
479479
return e.ID
480480
}
481481

482+
// EmojiParams represents parameters needed to create or update an Emoji.
483+
type EmojiParams struct {
484+
// Name of the emoji
485+
Name string `json:"name,omitempty"`
486+
// A base64 encoded emoji image, has to be smaller than 256KB.
487+
// NOTE: can be only set on creation.
488+
Image string `json:"image,omitempty"`
489+
// Roles for which this emoji will be available.
490+
Roles []string `json:"roles,omitempty"`
491+
}
492+
482493
// StickerFormat is the file format of the Sticker.
483494
type StickerFormat int
484495

@@ -972,6 +983,14 @@ type GuildTemplate struct {
972983
IsDirty bool `json:"is_dirty"`
973984
}
974985

986+
// GuildTemplateParams stores the data needed to create or update a GuildTemplate.
987+
type GuildTemplateParams struct {
988+
// The name of the template (1-100 characters)
989+
Name string `json:"name,omitempty"`
990+
// The description of the template (0-120 characters)
991+
Description string `json:"description,omitempty"`
992+
}
993+
975994
// MessageNotifications is the notification level for a guild
976995
// https://discord.com/developers/docs/resources/guild#guild-object-default-message-notification-level
977996
type MessageNotifications int
@@ -1100,6 +1119,20 @@ func (r *Role) Mention() string {
11001119
return fmt.Sprintf("<@&%s>", r.ID)
11011120
}
11021121

1122+
// RoleParams represents the parameters needed to create or update a Role
1123+
type RoleParams struct {
1124+
// The role's name
1125+
Name string `json:"name,omitempty"`
1126+
// The color the role should have (as a decimal, not hex)
1127+
Color *int `json:"color,omitempty"`
1128+
// Whether to display the role's users separately
1129+
Hoist *bool `json:"hoist,omitempty"`
1130+
// The overall permissions number of the role
1131+
Permissions *int64 `json:"permissions,omitempty,string"`
1132+
// Whether this role is mentionable
1133+
Mentionable *bool `json:"mentionable,omitempty"`
1134+
}
1135+
11031136
// Roles are a collection of Role
11041137
type Roles []*Role
11051138

@@ -1372,8 +1405,8 @@ type AutoModerationAction struct {
13721405

13731406
// A GuildEmbed stores data for a guild embed.
13741407
type GuildEmbed struct {
1375-
Enabled bool `json:"enabled"`
1376-
ChannelID string `json:"channel_id"`
1408+
Enabled *bool `json:"enabled,omitempty"`
1409+
ChannelID string `json:"channel_id,omitempty"`
13771410
}
13781411

13791412
// A GuildAuditLog stores data for a guild audit log.
@@ -1701,6 +1734,21 @@ func (p GuildMemberParams) MarshalJSON() (res []byte, err error) {
17011734
return json.Marshal(v)
17021735
}
17031736

1737+
// GuildMemberAddParams stores data needed to add a user to a guild.
1738+
// NOTE: All fields are optional, except AccessToken.
1739+
type GuildMemberAddParams struct {
1740+
// Valid access_token for the user.
1741+
AccessToken string `json:"access_token"`
1742+
// Value to set users nickname to.
1743+
Nick string `json:"nick,omitempty"`
1744+
// A list of role ID's to set on the member.
1745+
Roles []string `json:"roles,omitempty"`
1746+
// Whether the user is muted.
1747+
Mute bool `json:"mute,omitempty"`
1748+
// Whether the user is deafened.
1749+
Deaf bool `json:"deaf,omitempty"`
1750+
}
1751+
17041752
// An APIErrorMessage is an api error message returned from discord
17051753
type APIErrorMessage struct {
17061754
Code int `json:"code"`

0 commit comments

Comments
 (0)