Skip to content

Commit

Permalink
fix invite create event
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Aug 12, 2024
1 parent f24ca9a commit 0f6d90b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
39 changes: 28 additions & 11 deletions events/guild_invite_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,45 @@ import (
"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/gateway"
)

// GenericInvite is called upon receiving InviteCreate or InviteDelete (requires gateway.IntentGuildInvites)
type GenericInvite struct {
// InviteCreate is called upon creation of a new discord.Invite (requires gateway.IntentGuildInvites)
type InviteCreate struct {
*GenericEvent
GuildID *snowflake.ID
ChannelID snowflake.ID
Code string

gateway.EventInviteCreate
}

// Channel returns the discord.GuildChannel the GenericInvite happened in.
func (e *GenericInvite) Channel() (discord.GuildChannel, bool) {
func (e *InviteCreate) Channel() (discord.GuildChannel, bool) {
return e.Client().Caches().Channel(e.ChannelID)
}

// InviteCreate is called upon creation of a new discord.Invite (requires gateway.IntentGuildInvites)
type InviteCreate struct {
*GenericInvite
Invite discord.Invite
func (e *InviteCreate) Guild() (discord.Guild, bool) {
if e.GuildID == nil {
return discord.Guild{}, false
}
return e.Client().Caches().Guild(*e.GuildID)
}

// InviteDelete is called upon deletion of a discord.Invite (requires gateway.IntentGuildInvites)
type InviteDelete struct {
*GenericInvite
*GenericEvent

GuildID *snowflake.ID
ChannelID snowflake.ID
Code string
}

// Channel returns the discord.GuildChannel the GenericInvite happened in.
func (e *InviteDelete) Channel() (discord.GuildChannel, bool) {
return e.Client().Caches().Channel(e.ChannelID)
}

func (e *InviteDelete) Guild() (discord.Guild, bool) {
if e.GuildID == nil {
return discord.Guild{}, false
}
return e.Client().Caches().Guild(*e.GuildID)
}
13 changes: 12 additions & 1 deletion gateway/gateway_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,18 @@ func (EventInteractionCreate) messageData() {}
func (EventInteractionCreate) eventData() {}

type EventInviteCreate struct {
discord.Invite
ChannelID snowflake.ID `json:"channel_id"`
Code string `json:"code"`
CreatedAt time.Time `json:"created_at"`
GuildID *snowflake.ID `json:"guild_id"`
Inviter *discord.User `json:"inviter"`
MaxAge int `json:"max_age"`
MaxUses int `json:"max_uses"`
TargetType discord.InviteTargetType `json:"target_type"`
TargetUser *discord.User `json:"target_user"`
TargetApplication *discord.PartialApplication `json:"target_application"`
Temporary bool `json:"temporary"`
Uses int `json:"uses"`
}

func (EventInviteCreate) messageData() {}
Expand Down
26 changes: 6 additions & 20 deletions handlers/invite_handlers.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
package handlers

import (
"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
)

func gatewayHandlerInviteCreate(client bot.Client, sequenceNumber int, shardID int, event gateway.EventInviteCreate) {
var guildID *snowflake.ID
if event.Guild != nil {
guildID = &event.Guild.ID
}

client.EventManager().DispatchEvent(&events.InviteCreate{
GenericInvite: &events.GenericInvite{
GenericEvent: events.NewGenericEvent(client, sequenceNumber, shardID),
GuildID: guildID,
Code: event.Code,
ChannelID: event.ChannelID,
},
Invite: event.Invite,
GenericEvent: events.NewGenericEvent(client, sequenceNumber, shardID),
EventInviteCreate: event,
})
}

func gatewayHandlerInviteDelete(client bot.Client, sequenceNumber int, shardID int, event gateway.EventInviteDelete) {
client.EventManager().DispatchEvent(&events.InviteDelete{
GenericInvite: &events.GenericInvite{
GenericEvent: events.NewGenericEvent(client, sequenceNumber, shardID),
GuildID: event.GuildID,
ChannelID: event.ChannelID,
Code: event.Code,
},
GenericEvent: events.NewGenericEvent(client, sequenceNumber, shardID),
GuildID: event.GuildID,
ChannelID: event.ChannelID,
Code: event.Code,
})
}

0 comments on commit 0f6d90b

Please sign in to comment.