Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add newly_created field to EventThreadCreate #396

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 := json.Marshal(e.GuildThread)
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
Loading