From ef9df144e2864154fb57673c3d2a6b97d3a3b415 Mon Sep 17 00:00:00 2001 From: caneleex Date: Thu, 8 Dec 2022 23:20:23 +0100 Subject: [PATCH 1/4] adds some missing guild endpoints --- discord/guild.go | 19 +++++++++++++---- discord/invite.go | 5 +++++ rest/guilds.go | 46 ++++++++++++++++++++++++++++++++++++++++-- rest/rest_endpoints.go | 3 +++ 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/discord/guild.go b/discord/guild.go index ca133a8e..45efd9d3 100644 --- a/discord/guild.go +++ b/discord/guild.go @@ -164,7 +164,7 @@ type Guild struct { PreferredLocale string `json:"preferred_locale"` PublicUpdatesChannelID *snowflake.ID `json:"public_updates_channel_id"` MaxVideoChannelUsers int `json:"max_video_channel_users"` - WelcomeScreen WelcomeScreen `json:"welcome_screen"` + WelcomeScreen GuildWelcomeScreen `json:"welcome_screen"` NSFWLevel NSFWLevel `json:"nsfw_level"` BoostProgressBarEnabled bool `json:"premium_progress_bar_enabled"` JoinedAt time.Time `json:"joined_at"` @@ -265,13 +265,13 @@ type OAuth2Guild struct { Features []GuildFeature `json:"features"` } -// WelcomeScreen is the Welcome Screen of a Guild -type WelcomeScreen struct { +// GuildWelcomeScreen is the Welcome Screen of a Guild +type GuildWelcomeScreen struct { Description *string `json:"description,omitempty"` WelcomeChannels []GuildWelcomeChannel `json:"welcome_channels"` } -// GuildWelcomeChannel is one of the channels in a WelcomeScreen +// GuildWelcomeChannel is one of the channels in a GuildWelcomeScreen type GuildWelcomeChannel struct { ChannelID snowflake.ID `json:"channel_id"` Description string `json:"description"` @@ -279,6 +279,13 @@ type GuildWelcomeChannel struct { EmojiName *string `json:"emoji_name,omitempty"` } +// GuildWelcomeScreenUpdate is used to update the GuildWelcomeScreen of a Guild +type GuildWelcomeScreenUpdate struct { + Enabled *bool `json:"enabled,omitempty"` + WelcomeChannels *[]GuildWelcomeChannel `json:"welcome_channels,omitempty"` + Description *string `json:"description,omitempty"` +} + // GuildPreview is used for previewing public Guild(s) before joining them type GuildPreview struct { ID snowflake.ID `json:"id"` @@ -350,3 +357,7 @@ type GuildCreateChannel struct { ID int `json:"id,omitempty"` ParentID int `json:"parent_id,omitempty"` } + +type GuildPruneResult struct { + Pruned *int `json:"pruned"` +} diff --git a/discord/invite.go b/discord/invite.go index b59936f3..b8aaae5b 100644 --- a/discord/invite.go +++ b/discord/invite.go @@ -43,6 +43,11 @@ type ExtendedInvite struct { CreatedAt time.Time `json:"created_at"` } +type PartialInvite struct { + Code *string `json:"code"` + Uses int `json:"uses"` +} + type InviteChannel struct { ID snowflake.ID `json:"id"` Type ChannelType `json:"type"` diff --git a/rest/guilds.go b/rest/guilds.go index 16df5f7e..c4587c74 100644 --- a/rest/guilds.go +++ b/rest/guilds.go @@ -40,10 +40,18 @@ type Guilds interface { GetIntegrations(guildID snowflake.ID, opts ...RequestOpt) ([]discord.Integration, error) DeleteIntegration(guildID snowflake.ID, integrationID snowflake.ID, opts ...RequestOpt) error + GetPruneMembersCount(guildID snowflake.ID, days int, includeRoles string, opts ...RequestOpt) (discord.GuildPruneResult, error) + PruneMembers(guildID snowflake.ID, days int, computePruneCount bool, includeRoles string, opts ...RequestOpt) (discord.GuildPruneResult, error) + GetAllWebhooks(guildID snowflake.ID, opts ...RequestOpt) ([]discord.Webhook, error) + GetGuildVoiceRegions(guild snowflake.ID, opts ...RequestOpt) ([]discord.VoiceRegion, error) + GetAuditLog(guildID snowflake.ID, userID snowflake.ID, actionType discord.AuditLogEvent, before snowflake.ID, limit int, opts ...RequestOpt) (*discord.AuditLog, error) GetAuditLogPage(guildID snowflake.ID, userID snowflake.ID, actionType discord.AuditLogEvent, startID snowflake.ID, limit int, opts ...RequestOpt) AuditLogPage + + GetGuildWelcomeScreen(guildID snowflake.ID, opts ...RequestOpt) (*discord.GuildWelcomeScreen, error) + UpdateGuildWelcomeScreen(guildID snowflake.ID, screenUpdate discord.GuildWelcomeScreenUpdate, opts ...RequestOpt) (*discord.GuildWelcomeScreen, error) } type guildImpl struct { @@ -77,6 +85,11 @@ func (s *guildImpl) DeleteGuild(guildID snowflake.ID, opts ...RequestOpt) error return s.client.Do(DeleteGuild.Compile(nil, guildID), nil, nil, opts...) } +func (s *guildImpl) GetGuildVanityURL(guildID snowflake.ID, opts ...RequestOpt) (partialInvite *discord.PartialInvite, err error) { + err = s.client.Do(GetGuildVanityURL.Compile(nil, guildID), nil, &partialInvite, opts...) + return +} + func (s *guildImpl) CreateGuildChannel(guildID snowflake.ID, guildChannelCreate discord.GuildChannelCreate, opts ...RequestOpt) (guildChannel discord.GuildChannel, err error) { var ch discord.UnmarshalChannel err = s.client.Do(CreateGuildChannel.Compile(nil, guildID), guildChannelCreate, &ch, opts...) @@ -180,13 +193,32 @@ func (s *guildImpl) DeleteIntegration(guildID snowflake.ID, integrationID snowfl return s.client.Do(DeleteIntegration.Compile(nil, guildID, integrationID), nil, nil, opts...) } +func (s *guildImpl) GetPruneMembersCount(guildID snowflake.ID, days int, includeRoles string, opts ...RequestOpt) (result discord.GuildPruneResult, err error) { + values := discord.QueryValues{ + "days": days, + "include_roles": includeRoles, + } + err = s.client.Do(GetPruneMembersCount.Compile(values, guildID), nil, &result, opts...) + return +} + +func (s *guildImpl) PruneMembers(guildID snowflake.ID, days int, computePruneCount bool, includeRoles string, opts ...RequestOpt) (result discord.GuildPruneResult, err error) { + values := discord.QueryValues{ + "days": days, + "compute_prune_count": computePruneCount, + "include_roles": includeRoles, + } + err = s.client.Do(PruneMembers.Compile(values, guildID), nil, &result, opts...) + return +} + func (s *guildImpl) GetAllWebhooks(guildID snowflake.ID, opts ...RequestOpt) (webhooks []discord.Webhook, err error) { err = s.client.Do(GetGuildWebhooks.Compile(nil, guildID), nil, &webhooks, opts...) return } -func (s *guildImpl) GetEmojis(guildID snowflake.ID, opts ...RequestOpt) (emojis []discord.Emoji, err error) { - err = s.client.Do(GetEmojis.Compile(nil, guildID), nil, &emojis, opts...) +func (s *guildImpl) GetGuildVoiceRegions(guild snowflake.ID, opts ...RequestOpt) (regions []discord.VoiceRegion, err error) { + err = s.client.Do(GetGuildVoiceRegions.Compile(nil, guild), nil, ®ions, opts...) return } @@ -221,3 +253,13 @@ func (s *guildImpl) GetAuditLogPage(guildID snowflake.ID, userID snowflake.ID, a ID: startID, } } + +func (s *guildImpl) GetGuildWelcomeScreen(guildID snowflake.ID, opts ...RequestOpt) (welcomeScreen *discord.GuildWelcomeScreen, err error) { + err = s.client.Do(GetGuildWelcomeScreen.Compile(nil, guildID), nil, &welcomeScreen, opts...) + return +} + +func (s *guildImpl) UpdateGuildWelcomeScreen(guildID snowflake.ID, screenUpdate discord.GuildWelcomeScreenUpdate, opts ...RequestOpt) (welcomeScreen *discord.GuildWelcomeScreen, err error) { + err = s.client.Do(UpdateGuildWelcomeScreen.Compile(nil, guildID), screenUpdate, &welcomeScreen, opts...) + return +} diff --git a/rest/rest_endpoints.go b/rest/rest_endpoints.go index c63b1be3..5347739b 100644 --- a/rest/rest_endpoints.go +++ b/rest/rest_endpoints.go @@ -84,6 +84,9 @@ var ( GetGuildVoiceRegions = NewEndpoint(http.MethodGet, "/guilds/{guild.id}/regions") + GetGuildWelcomeScreen = NewEndpoint(http.MethodGet, "/guilds/{guild.id}/welcome-screen") + UpdateGuildWelcomeScreen = NewEndpoint(http.MethodPatch, "/guilds/{guild.id}/welcome-screen") + UpdateCurrentUserVoiceState = NewEndpoint(http.MethodPatch, "/guilds/{guild.id}/voice-states/@me") UpdateUserVoiceState = NewEndpoint(http.MethodPatch, "/guilds/{guild.id}/voice-states/{user.id}") ) From b2bdd2fd82edfba9a19640d0ec03b008480cf8a9 Mon Sep 17 00:00:00 2001 From: caneleex Date: Thu, 8 Dec 2022 23:22:30 +0100 Subject: [PATCH 2/4] add missing method specification --- rest/guilds.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rest/guilds.go b/rest/guilds.go index c4587c74..8fe07dea 100644 --- a/rest/guilds.go +++ b/rest/guilds.go @@ -20,6 +20,8 @@ type Guilds interface { UpdateGuild(guildID snowflake.ID, guildUpdate discord.GuildUpdate, opts ...RequestOpt) (*discord.RestGuild, error) DeleteGuild(guildID snowflake.ID, opts ...RequestOpt) error + GetGuildVanityURL(guildID snowflake.ID, opts ...RequestOpt) (*discord.PartialInvite, error) + CreateGuildChannel(guildID snowflake.ID, guildChannelCreate discord.GuildChannelCreate, opts ...RequestOpt) (discord.GuildChannel, error) GetGuildChannels(guildID snowflake.ID, opts ...RequestOpt) ([]discord.GuildChannel, error) UpdateChannelPositions(guildID snowflake.ID, guildChannelPositionUpdates []discord.GuildChannelPositionUpdate, opts ...RequestOpt) error From 0e30e84bb6d9bbd309e304f5f031b0b23c0988b9 Mon Sep 17 00:00:00 2001 From: caneleex Date: Thu, 8 Dec 2022 23:26:08 +0100 Subject: [PATCH 3/4] rename guildID param in GetGuildVoiceRegions --- rest/guilds.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rest/guilds.go b/rest/guilds.go index 8fe07dea..39b29285 100644 --- a/rest/guilds.go +++ b/rest/guilds.go @@ -47,7 +47,7 @@ type Guilds interface { GetAllWebhooks(guildID snowflake.ID, opts ...RequestOpt) ([]discord.Webhook, error) - GetGuildVoiceRegions(guild snowflake.ID, opts ...RequestOpt) ([]discord.VoiceRegion, error) + GetGuildVoiceRegions(guildID snowflake.ID, opts ...RequestOpt) ([]discord.VoiceRegion, error) GetAuditLog(guildID snowflake.ID, userID snowflake.ID, actionType discord.AuditLogEvent, before snowflake.ID, limit int, opts ...RequestOpt) (*discord.AuditLog, error) GetAuditLogPage(guildID snowflake.ID, userID snowflake.ID, actionType discord.AuditLogEvent, startID snowflake.ID, limit int, opts ...RequestOpt) AuditLogPage @@ -219,8 +219,8 @@ func (s *guildImpl) GetAllWebhooks(guildID snowflake.ID, opts ...RequestOpt) (we return } -func (s *guildImpl) GetGuildVoiceRegions(guild snowflake.ID, opts ...RequestOpt) (regions []discord.VoiceRegion, err error) { - err = s.client.Do(GetGuildVoiceRegions.Compile(nil, guild), nil, ®ions, opts...) +func (s *guildImpl) GetGuildVoiceRegions(guildID snowflake.ID, opts ...RequestOpt) (regions []discord.VoiceRegion, err error) { + err = s.client.Do(GetGuildVoiceRegions.Compile(nil, guildID), nil, ®ions, opts...) return } From 1340ca9164347dcd599b2e3c04926351b056e995 Mon Sep 17 00:00:00 2001 From: caneleex Date: Thu, 8 Dec 2022 23:53:31 +0100 Subject: [PATCH 4/4] address reviews --- discord/guild.go | 6 ++++++ rest/guilds.go | 28 +++++++++++++++------------- rest/rest_endpoints.go | 4 ++-- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/discord/guild.go b/discord/guild.go index 45efd9d3..d1f92c13 100644 --- a/discord/guild.go +++ b/discord/guild.go @@ -358,6 +358,12 @@ type GuildCreateChannel struct { ParentID int `json:"parent_id,omitempty"` } +type GuildPrune struct { + Days int `json:"days"` + ComputePruneCount bool `json:"compute_prune_count"` + IncludeRoles []snowflake.ID `json:"include_roles"` +} + type GuildPruneResult struct { Pruned *int `json:"pruned"` } diff --git a/rest/guilds.go b/rest/guilds.go index 39b29285..e8ee9429 100644 --- a/rest/guilds.go +++ b/rest/guilds.go @@ -42,8 +42,8 @@ type Guilds interface { GetIntegrations(guildID snowflake.ID, opts ...RequestOpt) ([]discord.Integration, error) DeleteIntegration(guildID snowflake.ID, integrationID snowflake.ID, opts ...RequestOpt) error - GetPruneMembersCount(guildID snowflake.ID, days int, includeRoles string, opts ...RequestOpt) (discord.GuildPruneResult, error) - PruneMembers(guildID snowflake.ID, days int, computePruneCount bool, includeRoles string, opts ...RequestOpt) (discord.GuildPruneResult, error) + GetGuildPruneCount(guildID snowflake.ID, days int, includeRoles []snowflake.ID, opts ...RequestOpt) (*discord.GuildPruneResult, error) + BeginGuildPrune(guildID snowflake.ID, guildPrune discord.GuildPrune, opts ...RequestOpt) (*discord.GuildPruneResult, error) GetAllWebhooks(guildID snowflake.ID, opts ...RequestOpt) ([]discord.Webhook, error) @@ -195,22 +195,24 @@ func (s *guildImpl) DeleteIntegration(guildID snowflake.ID, integrationID snowfl return s.client.Do(DeleteIntegration.Compile(nil, guildID, integrationID), nil, nil, opts...) } -func (s *guildImpl) GetPruneMembersCount(guildID snowflake.ID, days int, includeRoles string, opts ...RequestOpt) (result discord.GuildPruneResult, err error) { +func (s *guildImpl) GetGuildPruneCount(guildID snowflake.ID, days int, includeRoles []snowflake.ID, opts ...RequestOpt) (result *discord.GuildPruneResult, err error) { values := discord.QueryValues{ - "days": days, - "include_roles": includeRoles, + "days": days, } - err = s.client.Do(GetPruneMembersCount.Compile(values, guildID), nil, &result, opts...) + var joinedRoles string + for i, roleID := range includeRoles { + joinedRoles += roleID.String() + if i != len(includeRoles)-1 { + joinedRoles += "," + } + } + values["include_roles"] = joinedRoles + err = s.client.Do(GetGuildPruneCount.Compile(values, guildID), nil, &result, opts...) return } -func (s *guildImpl) PruneMembers(guildID snowflake.ID, days int, computePruneCount bool, includeRoles string, opts ...RequestOpt) (result discord.GuildPruneResult, err error) { - values := discord.QueryValues{ - "days": days, - "compute_prune_count": computePruneCount, - "include_roles": includeRoles, - } - err = s.client.Do(PruneMembers.Compile(values, guildID), nil, &result, opts...) +func (s *guildImpl) BeginGuildPrune(guildID snowflake.ID, guildPrune discord.GuildPrune, opts ...RequestOpt) (result *discord.GuildPruneResult, err error) { + err = s.client.Do(BeginGuildPrune.Compile(nil, guildID), guildPrune, &result, opts...) return } diff --git a/rest/rest_endpoints.go b/rest/rest_endpoints.go index 5347739b..469898a4 100644 --- a/rest/rest_endpoints.go +++ b/rest/rest_endpoints.go @@ -75,8 +75,8 @@ var ( UpdateCurrentMember = NewEndpoint(http.MethodPatch, "/guilds/{guild.id}/members/@me") - GetPruneMembersCount = NewEndpoint(http.MethodGet, "/guilds/{guild.id}/prune") - PruneMembers = NewEndpoint(http.MethodPost, "/guilds/{guild.id}/prune") + GetGuildPruneCount = NewEndpoint(http.MethodGet, "/guilds/{guild.id}/prune") + BeginGuildPrune = NewEndpoint(http.MethodPost, "/guilds/{guild.id}/prune") GetGuildWebhooks = NewEndpoint(http.MethodGet, "/guilds/{guild.id}/webhooks")