From 6147e0a57027f03e642e2b8d6ea4870cd8212d11 Mon Sep 17 00:00:00 2001 From: topi314 Date: Wed, 16 Oct 2024 12:08:36 +0200 Subject: [PATCH] add newly_created field to EventThreadCreate & fix broken unmarshalling of EventThreadCreate, EventIntegrationUpdate & EventIntegrationCreate --- _examples/threads/example.go | 2 +- events/guild_thread_events.go | 1 + gateway/gateway_events.go | 108 +++++++++++++++++++++++++++++----- handlers/thread_handler.go | 1 + 4 files changed, 97 insertions(+), 15 deletions(-) diff --git a/_examples/threads/example.go b/_examples/threads/example.go index 6db639aa3..c79bd5dc1 100644 --- a/_examples/threads/example.go +++ b/_examples/threads/example.go @@ -38,7 +38,7 @@ func main() { } }, OnThreadCreate: func(event *events.ThreadCreate) { - slog.Info("ThreadCreateEvent") + slog.Info("ThreadCreateEvent", slog.Any("newly_created", event.NewlyCreated)) }, OnThreadUpdate: func(event *events.ThreadUpdate) { slog.Info("ThreadUpdateEvent") diff --git a/events/guild_thread_events.go b/events/guild_thread_events.go index 479fade92..60be737b9 100644 --- a/events/guild_thread_events.go +++ b/events/guild_thread_events.go @@ -19,6 +19,7 @@ type GenericThread struct { type ThreadCreate struct { *GenericThread ThreadMember discord.ThreadMember + NewlyCreated bool } // ThreadUpdate is dispatched when a thread is updated. diff --git a/gateway/gateway_events.go b/gateway/gateway_events.go index 2d77af4a3..ea5c93f25 100644 --- a/gateway/gateway_events.go +++ b/gateway/gateway_events.go @@ -101,6 +101,48 @@ func (EventChannelDelete) eventData() {} type EventThreadCreate struct { discord.GuildThread ThreadMember discord.ThreadMember `json:"thread_member"` + NewlyCreated bool `json:"newly_created"` +} + +func (e *EventThreadCreate) UnmarshalJSON(data []byte) error { + var guildThread discord.GuildThread + if err := json.Unmarshal(data, &guildThread); err != nil { + return err + } + + e.GuildThread = guildThread + + var v struct { + ThreadMember discord.ThreadMember `json:"thread_member"` + NewlyCreated bool `json:"newly_created"` + } + if err := json.Unmarshal(data, &v); err != nil { + return err + } + + e.ThreadMember = v.ThreadMember + e.NewlyCreated = v.NewlyCreated + return nil +} + +func (e EventThreadCreate) MarshalJSON() ([]byte, error) { + data1, err := e.MarshalJSON() + if err != nil { + return nil, err + } + + data2, err := json.Marshal(struct { + ThreadMember discord.ThreadMember `json:"thread_member"` + NewlyCreated bool `json:"newly_created"` + }{ + ThreadMember: e.ThreadMember, + NewlyCreated: e.NewlyCreated, + }) + if err != nil { + return nil, err + } + + return json.SimpleMerge(data1, data2) } func (EventThreadCreate) messageData() {} @@ -707,22 +749,41 @@ type EventIntegrationCreate struct { } func (e *EventIntegrationCreate) UnmarshalJSON(data []byte) error { - type integrationCreateEvent EventIntegrationCreate - var v struct { - discord.UnmarshalIntegration - integrationCreateEvent + var integration discord.UnmarshalIntegration + if err := json.Unmarshal(data, &integration); err != nil { + return err } + var v struct { + GuildID snowflake.ID `json:"guild_id"` + } if err := json.Unmarshal(data, &v); err != nil { return err } - *e = EventIntegrationCreate(v.integrationCreateEvent) - - e.Integration = v.UnmarshalIntegration.Integration + e.Integration = integration.Integration + e.GuildID = v.GuildID return nil } +func (e EventIntegrationCreate) MarshalJSON() ([]byte, error) { + data1, err := json.Marshal(e.Integration) + if err != nil { + return nil, err + } + + data2, err := json.Marshal(struct { + GuildID snowflake.ID `json:"guild_id"` + }{ + GuildID: e.GuildID, + }) + if err != nil { + return nil, err + } + + return json.SimpleMerge(data1, data2) +} + func (EventIntegrationCreate) messageData() {} func (EventIntegrationCreate) eventData() {} @@ -732,22 +793,41 @@ type EventIntegrationUpdate struct { } func (e *EventIntegrationUpdate) UnmarshalJSON(data []byte) error { - type integrationUpdateEvent EventIntegrationUpdate - var v struct { - discord.UnmarshalIntegration - integrationUpdateEvent + var integration discord.UnmarshalIntegration + if err := json.Unmarshal(data, &integration); err != nil { + return err } + var v struct { + GuildID snowflake.ID `json:"guild_id"` + } if err := json.Unmarshal(data, &v); err != nil { return err } - *e = EventIntegrationUpdate(v.integrationUpdateEvent) - - e.Integration = v.UnmarshalIntegration.Integration + e.Integration = integration.Integration + e.GuildID = v.GuildID return nil } +func (e EventIntegrationUpdate) MarshalJSON() ([]byte, error) { + data1, err := json.Marshal(e.Integration) + if err != nil { + return nil, err + } + + data2, err := json.Marshal(struct { + GuildID snowflake.ID `json:"guild_id"` + }{ + GuildID: e.GuildID, + }) + if err != nil { + return nil, err + } + + return json.SimpleMerge(data1, data2) +} + func (EventIntegrationUpdate) messageData() {} func (EventIntegrationUpdate) eventData() {} diff --git a/handlers/thread_handler.go b/handlers/thread_handler.go index 3058ddf3e..f77b23353 100644 --- a/handlers/thread_handler.go +++ b/handlers/thread_handler.go @@ -19,6 +19,7 @@ func gatewayHandlerThreadCreate(client bot.Client, sequenceNumber int, shardID i Thread: event.GuildThread, }, ThreadMember: event.ThreadMember, + NewlyCreated: event.NewlyCreated, }) }