From 6d9cb99d48fa6ca10ad63db6532e1a91839538e2 Mon Sep 17 00:00:00 2001 From: TopiSenpai Date: Sun, 27 Mar 2022 16:42:36 +0200 Subject: [PATCH 1/4] make voice channels guild message channels --- discord/channel.go | 92 +++++++++++++++++++++++++++++------------ discord/channels_raw.go | 27 +++++++----- 2 files changed, 81 insertions(+), 38 deletions(-) diff --git a/discord/channel.go b/discord/channel.go index a64c6dcc..629d169a 100644 --- a/discord/channel.go +++ b/discord/channel.go @@ -331,22 +331,28 @@ func (DMChannel) channel() {} func (DMChannel) messageChannel() {} var ( - _ Channel = (*GuildVoiceChannel)(nil) - _ GuildChannel = (*GuildVoiceChannel)(nil) - _ GuildAudioChannel = (*GuildVoiceChannel)(nil) + _ Channel = (*GuildVoiceChannel)(nil) + _ GuildChannel = (*GuildVoiceChannel)(nil) + _ GuildAudioChannel = (*GuildVoiceChannel)(nil) + _ GuildMessageChannel = (*GuildVoiceChannel)(nil) ) type GuildVoiceChannel struct { - id snowflake.Snowflake - guildID snowflake.Snowflake - position int - permissionOverwrites []PermissionOverwrite - name string - bitrate int - UserLimit int - parentID *snowflake.Snowflake - rtcRegion string - VideoQualityMode VideoQualityMode + id snowflake.Snowflake + guildID snowflake.Snowflake + position int + permissionOverwrites []PermissionOverwrite + name string + bitrate int + UserLimit int + parentID *snowflake.Snowflake + rtcRegion string + VideoQualityMode VideoQualityMode + lastMessageID *snowflake.Snowflake + lastPinTimestamp *Time + topic *string + nsfw bool + defaultAutoArchiveDuration AutoArchiveDuration } func (c *GuildVoiceChannel) UnmarshalJSON(data []byte) error { @@ -365,22 +371,32 @@ func (c *GuildVoiceChannel) UnmarshalJSON(data []byte) error { c.parentID = v.ParentID c.rtcRegion = v.RTCRegion c.VideoQualityMode = v.VideoQualityMode + c.lastMessageID = v.LastMessageID + c.lastPinTimestamp = v.LastPinTimestamp + c.topic = v.Topic + c.nsfw = v.NSFW + c.defaultAutoArchiveDuration = v.DefaultAutoArchiveDuration return nil } func (c GuildVoiceChannel) MarshalJSON() ([]byte, error) { return json.Marshal(guildVoiceChannel{ - ID: c.id, - Type: c.Type(), - GuildID: c.guildID, - Position: c.position, - PermissionOverwrites: c.permissionOverwrites, - Name: c.name, - Bitrate: c.bitrate, - UserLimit: c.UserLimit, - ParentID: c.parentID, - RTCRegion: c.rtcRegion, - VideoQualityMode: c.VideoQualityMode, + ID: c.id, + Type: c.Type(), + GuildID: c.guildID, + Position: c.position, + PermissionOverwrites: c.permissionOverwrites, + Name: c.name, + Bitrate: c.bitrate, + UserLimit: c.UserLimit, + ParentID: c.parentID, + RTCRegion: c.rtcRegion, + VideoQualityMode: c.VideoQualityMode, + LastMessageID: c.lastMessageID, + LastPinTimestamp: c.lastPinTimestamp, + Topic: c.topic, + NSFW: c.nsfw, + DefaultAutoArchiveDuration: c.defaultAutoArchiveDuration, }) } @@ -428,9 +444,31 @@ func (c GuildVoiceChannel) ParentID() *snowflake.Snowflake { return c.parentID } -func (GuildVoiceChannel) channel() {} -func (GuildVoiceChannel) guildChannel() {} -func (GuildVoiceChannel) guildAudioChannel() {} +func (c GuildVoiceChannel) LastMessageID() *snowflake.Snowflake { + return c.lastMessageID +} + +func (c GuildVoiceChannel) LastPinTimestamp() *Time { + return c.lastPinTimestamp +} + +func (c GuildVoiceChannel) Topic() *string { + return c.topic +} + +func (c GuildVoiceChannel) NSFW() bool { + return c.nsfw +} + +func (c GuildVoiceChannel) DefaultAutoArchiveDuration() AutoArchiveDuration { + return c.defaultAutoArchiveDuration +} + +func (GuildVoiceChannel) channel() {} +func (GuildVoiceChannel) messageChannel() {} +func (GuildVoiceChannel) guildChannel() {} +func (GuildVoiceChannel) guildAudioChannel() {} +func (GuildVoiceChannel) guildMessageChannel() {} var ( _ Channel = (*GuildCategoryChannel)(nil) diff --git a/discord/channels_raw.go b/discord/channels_raw.go index 8d0dc08c..fecd6b76 100644 --- a/discord/channels_raw.go +++ b/discord/channels_raw.go @@ -128,17 +128,22 @@ func (t *guildCategoryChannel) UnmarshalJSON(data []byte) error { } type guildVoiceChannel struct { - ID snowflake.Snowflake `json:"id"` - Type ChannelType `json:"type"` - GuildID snowflake.Snowflake `json:"guild_id"` - Position int `json:"position"` - PermissionOverwrites []PermissionOverwrite `json:"permission_overwrites"` - Name string `json:"name"` - Bitrate int `json:"bitrate"` - UserLimit int `json:"user_limit"` - ParentID *snowflake.Snowflake `json:"parent_id"` - RTCRegion string `json:"rtc_region"` - VideoQualityMode VideoQualityMode `json:"video_quality_mode"` + ID snowflake.Snowflake `json:"id"` + Type ChannelType `json:"type"` + GuildID snowflake.Snowflake `json:"guild_id"` + Position int `json:"position"` + PermissionOverwrites []PermissionOverwrite `json:"permission_overwrites"` + Name string `json:"name"` + Bitrate int `json:"bitrate"` + UserLimit int `json:"user_limit"` + ParentID *snowflake.Snowflake `json:"parent_id"` + RTCRegion string `json:"rtc_region"` + VideoQualityMode VideoQualityMode `json:"video_quality_mode"` + LastMessageID *snowflake.Snowflake `json:"last_message_id"` + LastPinTimestamp *Time `json:"last_pin_timestamp"` + Topic *string `json:"topic"` + NSFW bool `json:"nsfw"` + DefaultAutoArchiveDuration AutoArchiveDuration `json:"default_auto_archive_duration"` } func (t *guildVoiceChannel) UnmarshalJSON(data []byte) error { From 40fcc0c817a85d759c9415b179fac196c8de5fff Mon Sep 17 00:00:00 2001 From: TopiSenpai Date: Sun, 27 Mar 2022 16:45:27 +0200 Subject: [PATCH 2/4] remove variable from method --- discord/channel.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/discord/channel.go b/discord/channel.go index 629d169a..bdc22d54 100644 --- a/discord/channel.go +++ b/discord/channel.go @@ -788,11 +788,11 @@ func (c GuildThread) DefaultAutoArchiveDuration() AutoArchiveDuration { return 0 } -func (GuildThread) channel() {} -func (GuildThread) guildChannel() {} -func (GuildThread) messageChannel() {} -func (c GuildThread) guildMessageChannel() {} -func (GuildThread) guildThread() {} +func (GuildThread) channel() {} +func (GuildThread) guildChannel() {} +func (GuildThread) messageChannel() {} +func (GuildThread) guildMessageChannel() {} +func (GuildThread) guildThread() {} var ( _ Channel = (*GuildStageVoiceChannel)(nil) From 2351211584c7f41e6f8978a03af46b73e6bb1063 Mon Sep 17 00:00:00 2001 From: TopiSenpai Date: Sun, 27 Mar 2022 22:24:39 +0200 Subject: [PATCH 3/4] move roles, emojis. stickers to different struct so those slices are not cached --- discord/guild.go | 12 ++++++++---- rest/guilds.go | 12 ++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/discord/guild.go b/discord/guild.go index 466b9e05..1b300abe 100644 --- a/discord/guild.go +++ b/discord/guild.go @@ -110,8 +110,6 @@ type Guild struct { VerificationLevel VerificationLevel `json:"verification_level"` DefaultMessageNotifications MessageNotificationsLevel `json:"default_message_notifications"` ExplicitContentFilter ExplicitContentFilterLevel `json:"explicit_content_filter"` - Roles []Role `json:"roles"` - Emojis []Emoji `json:"emojis"` Features []GuildFeature `json:"features"` MFALevel MFALevel `json:"mfa_level"` ApplicationID *snowflake.Snowflake `json:"application_id"` @@ -132,7 +130,6 @@ type Guild struct { WelcomeScreen WelcomeScreen `json:"welcome_screen"` NSFWLevel NSFWLevel `json:"nsfw_level"` BoostProgressBarEnabled bool `json:"premium_progress_bar_enabled"` - Stickers []Sticker `json:"stickers"` JoinedAt Time `json:"joined_at"` // only over GET /guilds/{guild.id} @@ -168,8 +165,15 @@ func (g Guild) BannerURL(opts ...CDNOpt) *string { return formatAssetURL(route.GuildBanner, opts, g.ID, *g.Banner) } -type GatewayGuild struct { +type RestGuild struct { Guild + Stickers []Sticker `json:"stickers"` + Roles []Role `json:"roles"` + Emojis []Emoji `json:"emojis"` +} + +type GatewayGuild struct { + RestGuild Large bool `json:"large"` Unavailable bool `json:"unavailable"` VoiceStates []VoiceState `json:"voice_states"` diff --git a/rest/guilds.go b/rest/guilds.go index 1dd9662b..fe32ecb7 100644 --- a/rest/guilds.go +++ b/rest/guilds.go @@ -13,10 +13,10 @@ func NewGuilds(restClient Client) Guilds { } type Guilds interface { - GetGuild(guildID snowflake.Snowflake, withCounts bool, opts ...RequestOpt) (*discord.Guild, error) + GetGuild(guildID snowflake.Snowflake, withCounts bool, opts ...RequestOpt) (*discord.RestGuild, error) GetGuildPreview(guildID snowflake.Snowflake, opts ...RequestOpt) (*discord.GuildPreview, error) - CreateGuild(guildCreate discord.GuildCreate, opts ...RequestOpt) (*discord.Guild, error) - UpdateGuild(guildID snowflake.Snowflake, guildUpdate discord.GuildUpdate, opts ...RequestOpt) (*discord.Guild, error) + CreateGuild(guildCreate discord.GuildCreate, opts ...RequestOpt) (*discord.RestGuild, error) + UpdateGuild(guildID snowflake.Snowflake, guildUpdate discord.GuildUpdate, opts ...RequestOpt) (*discord.RestGuild, error) DeleteGuild(guildID snowflake.Snowflake, opts ...RequestOpt) error CreateGuildChannel(guildID snowflake.Snowflake, guildChannelCreate discord.GuildChannelCreate, opts ...RequestOpt) (discord.GuildChannel, error) @@ -47,7 +47,7 @@ type guildImpl struct { restClient Client } -func (s *guildImpl) GetGuild(guildID snowflake.Snowflake, withCounts bool, opts ...RequestOpt) (guild *discord.Guild, err error) { +func (s *guildImpl) GetGuild(guildID snowflake.Snowflake, withCounts bool, opts ...RequestOpt) (guild *discord.RestGuild, err error) { values := route.QueryValues{} if withCounts { values["withCounts"] = true @@ -71,7 +71,7 @@ func (s *guildImpl) GetGuildPreview(guildID snowflake.Snowflake, opts ...Request return } -func (s *guildImpl) CreateGuild(guildCreate discord.GuildCreate, opts ...RequestOpt) (guild *discord.Guild, err error) { +func (s *guildImpl) CreateGuild(guildCreate discord.GuildCreate, opts ...RequestOpt) (guild *discord.RestGuild, err error) { var compiledRoute *route.CompiledAPIRoute compiledRoute, err = route.CreateGuild.Compile(nil) if err != nil { @@ -81,7 +81,7 @@ func (s *guildImpl) CreateGuild(guildCreate discord.GuildCreate, opts ...Request return } -func (s *guildImpl) UpdateGuild(guildID snowflake.Snowflake, guildUpdate discord.GuildUpdate, opts ...RequestOpt) (guild *discord.Guild, err error) { +func (s *guildImpl) UpdateGuild(guildID snowflake.Snowflake, guildUpdate discord.GuildUpdate, opts ...RequestOpt) (guild *discord.RestGuild, err error) { var compiledRoute *route.CompiledAPIRoute compiledRoute, err = route.UpdateGuild.Compile(nil, guildID) if err != nil { From 33d0ea6fd6fc409c20c7a64d8817bc1974051753 Mon Sep 17 00:00:00 2001 From: TopiSenpai Date: Wed, 1 Jun 2022 22:03:50 +0200 Subject: [PATCH 4/4] update MessageChannels LastMessageID --- discord/channel.go | 19 +++++++++++++++++++ handlers/message_create_handler.go | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/discord/channel.go b/discord/channel.go index f04f57f2..0d1d0d86 100644 --- a/discord/channel.go +++ b/discord/channel.go @@ -995,3 +995,22 @@ func ApplyGuildIDToChannel(channel GuildChannel, guildID snowflake.ID) GuildChan panic("unknown channel type") } } + +func ApplyLastMessageIDToChannel(channel MessageChannel, lastMessageID snowflake.ID) MessageChannel { + switch c := channel.(type) { + case GuildTextChannel: + c.lastMessageID = &lastMessageID + return c + case GuildVoiceChannel: + c.lastMessageID = &lastMessageID + return c + case GuildNewsChannel: + c.lastMessageID = &lastMessageID + return c + case GuildThread: + c.lastMessageID = &lastMessageID + return c + default: + panic("unknown channel type") + } +} diff --git a/handlers/message_create_handler.go b/handlers/message_create_handler.go index 04f2d712..21fb87e6 100644 --- a/handlers/message_create_handler.go +++ b/handlers/message_create_handler.go @@ -30,6 +30,10 @@ func (h *gatewayHandlerMessageCreate) HandleGatewayEvent(client bot.Client, sequ client.Caches().Messages().Put(message.ChannelID, message.ID, message) + if channel, ok := client.Caches().Channels().GetMessageChannel(message.ChannelID); ok { + client.Caches().Channels().Put(message.ChannelID, discord.ApplyLastMessageIDToChannel(channel, message.ID)) + } + genericEvent := events.NewGenericEvent(client, sequenceNumber, shardID) client.EventManager().DispatchEvent(&events.MessageCreate{ GenericMessage: &events.GenericMessage{