Skip to content

Commit

Permalink
add newly_created field to EventThreadCreate & fix broken unmarshalli…
Browse files Browse the repository at this point in the history
…ng of EventThreadCreate, EventIntegrationUpdate & EventIntegrationCreate
  • Loading branch information
topi314 committed Oct 16, 2024
1 parent 417678a commit 6147e0a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 15 deletions.
2 changes: 1 addition & 1 deletion _examples/threads/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions events/guild_thread_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type GenericThread struct {
type ThreadCreate struct {
*GenericThread
ThreadMember discord.ThreadMember
NewlyCreated bool
}

// ThreadUpdate is dispatched when a thread is updated.
Expand Down
108 changes: 94 additions & 14 deletions gateway/gateway_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Check failure on line 129 in gateway/gateway_events.go

View workflow job for this annotation

GitHub Actions / golangci

SA5007: infinite recursive call (staticcheck)
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() {}
Expand Down Expand Up @@ -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() {}

Expand All @@ -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() {}

Expand Down
1 change: 1 addition & 0 deletions handlers/thread_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func gatewayHandlerThreadCreate(client bot.Client, sequenceNumber int, shardID i
Thread: event.GuildThread,
},
ThreadMember: event.ThreadMember,
NewlyCreated: event.NewlyCreated,
})
}

Expand Down

0 comments on commit 6147e0a

Please sign in to comment.