Skip to content

Commit

Permalink
split poll events into DM and guild
Browse files Browse the repository at this point in the history
  • Loading branch information
mlnrDev committed Apr 13, 2024
1 parent 8a73887 commit e25946c
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 24 deletions.
24 changes: 24 additions & 0 deletions events/dm_message_poll_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package events

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

// GenericDMMessagePollVote is called upon receiving DMMessagePollVoteAdd or DMMessagePollVoteRemove (requires the gateway.IntentDirectMessagePolls)
type GenericDMMessagePollVote struct {
*GenericEvent
UserID snowflake.ID
ChannelID snowflake.ID
MessageID snowflake.ID
AnswerID int
}

// DMMessagePollVoteAdd indicates that a discord.User voted on a discord.Poll in a DM (requires gateway.IntentDirectMessagePolls)
type DMMessagePollVoteAdd struct {
*GenericDMMessagePollVote
}

// DMMessagePollVoteRemove indicates that a discord.User removed their vote on a discord.Poll in a DM (requires gateway.IntentDirectMessagePolls)
type DMMessagePollVoteRemove struct {
*GenericDMMessagePollVote
}
31 changes: 31 additions & 0 deletions events/guild_message_poll_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package events

import (
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/snowflake/v2"
)

// GenericGuildMessagePollVote is called upon receiving GuildMessagePollVoteAdd or GuildMessagePollVoteRemove (requires the gateway.IntentGuildMessagePolls)
type GenericGuildMessagePollVote struct {
*GenericEvent
UserID snowflake.ID
ChannelID snowflake.ID
MessageID snowflake.ID
GuildID snowflake.ID
AnswerID int
}

// Guild returns the discord.Guild where the GenericGuildMessagePollVote happened
func (e *GenericGuildMessagePollVote) Guild() (discord.Guild, bool) {
return e.Client().Caches().Guild(e.GuildID)
}

// GuildMessagePollVoteAdd indicates that a discord.User voted on a discord.Poll in a discord.Guild (requires gateway.IntentGuildMessagePolls)
type GuildMessagePollVoteAdd struct {
*GenericGuildMessagePollVote
}

// GuildMessagePollVoteRemove indicates that a discord.User removed their vote on a discord.Poll in a discord.Guild (requires gateway.IntentGuildMessagePolls)
type GuildMessagePollVoteRemove struct {
*GenericGuildMessagePollVote
}
24 changes: 24 additions & 0 deletions events/listener_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ type ListenerAdapter struct {
OnMessagePollVoteAdd func(event *MessagePollVoteAdd)
OnMessagePollVoteRemove func(event *MessagePollVoteRemove)

// DM Message Poll Events
OnDMMessagePollVoteAdd func(event *DMMessagePollVoteAdd)
OnDMMessagePollVoteRemove func(event *DMMessagePollVoteRemove)

// Guild Message Poll Events
OnGuildMessagePollVoteAdd func(event *GuildMessagePollVoteAdd)
OnGuildMessagePollVoteRemove func(event *GuildMessagePollVoteRemove)

// Message Reaction Events
OnMessageReactionAdd func(event *MessageReactionAdd)
OnMessageReactionRemove func(event *MessageReactionRemove)
Expand Down Expand Up @@ -587,6 +595,22 @@ func (l *ListenerAdapter) OnEvent(event bot.Event) {
if listener := l.OnMessagePollVoteRemove; listener != nil {
listener(e)
}
case *DMMessagePollVoteAdd:
if listener := l.OnDMMessagePollVoteAdd; listener != nil {
listener(e)
}
case *DMMessagePollVoteRemove:
if listener := l.OnDMMessagePollVoteRemove; listener != nil {
listener(e)
}
case *GuildMessagePollVoteAdd:
if listener := l.OnGuildMessagePollVoteAdd; listener != nil {
listener(e)
}
case *GuildMessagePollVoteRemove:
if listener := l.OnGuildMessagePollVoteRemove; listener != nil {
listener(e)
}

// Message Reaction Events
case *MessageReactionAdd:
Expand Down
20 changes: 0 additions & 20 deletions events/message_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,3 @@ type MessageUpdate struct {
type MessageDelete struct {
*GenericMessage
}

// GenericMessagePoll is a generic poll event (requires gateway.IntentGuildMessageReactions or gateway.IntentDirectMessageReactions)
type GenericMessagePoll struct {
*GenericEvent
UserID snowflake.ID
ChannelID snowflake.ID
MessageID snowflake.ID
GuildID *snowflake.ID
AnswerID int
}

// MessagePollVoteAdd indicates that a discord.User voted on a discord.Poll (requires gateway.IntentGuildMessageReactions or gateway.IntentDirectMessageReactions)
type MessagePollVoteAdd struct {
*GenericMessagePoll
}

// MessagePollVoteRemove indicates that a discord.User removed their vote on a discord.Poll (requires gateway.IntentGuildMessageReactions or gateway.IntentDirectMessageReactions)
type MessagePollVoteRemove struct {
*GenericMessagePoll
}
34 changes: 34 additions & 0 deletions events/message_poll_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package events

import (
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/snowflake/v2"
)

// GenericMessagePollVote is a generic poll vote event (requires gateway.IntentGuildMessagePolls or gateway.IntentDirectMessagePolls)
type GenericMessagePollVote struct {
*GenericEvent
UserID snowflake.ID
ChannelID snowflake.ID
MessageID snowflake.ID
GuildID *snowflake.ID
AnswerID int
}

// Guild returns the discord.Guild where the GenericMessagePoll happened or empty if it happened in DMs
func (e *GenericMessagePollVote) Guild() (discord.Guild, bool) {
if e.GuildID == nil {
return discord.Guild{}, false
}
return e.Client().Caches().Guild(*e.GuildID)
}

// MessagePollVoteAdd indicates that a discord.User voted on a discord.Poll (requires gateway.IntentGuildMessagePolls or gateway.IntentDirectMessagePolls)
type MessagePollVoteAdd struct {
*GenericMessagePollVote
}

// MessagePollVoteRemove indicates that a discord.User removed their vote on a discord.Poll (requires gateway.IntentGuildMessagePolls or gateway.IntentDirectMessagePolls)
type MessagePollVoteRemove struct {
*GenericMessagePollVote
}
58 changes: 54 additions & 4 deletions handlers/message_poll_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,77 @@ import (
)

func gatewayHandlerMessagePollVoteAdd(client bot.Client, sequenceNumber int, shardID int, event gateway.EventMessagePollVoteAdd) {
genericEvent := events.NewGenericEvent(client, sequenceNumber, shardID)

client.EventManager().DispatchEvent(&events.MessagePollVoteAdd{
GenericMessagePoll: &events.GenericMessagePoll{
GenericEvent: events.NewGenericEvent(client, sequenceNumber, shardID),
GenericMessagePollVote: &events.GenericMessagePollVote{
GenericEvent: genericEvent,
UserID: event.UserID,
ChannelID: event.ChannelID,
MessageID: event.MessageID,
GuildID: event.GuildID,
AnswerID: event.AnswerID,
},
})

if event.GuildID == nil {
client.EventManager().DispatchEvent(&events.DMMessagePollVoteAdd{
GenericDMMessagePollVote: &events.GenericDMMessagePollVote{
GenericEvent: genericEvent,
UserID: event.UserID,
ChannelID: event.ChannelID,
MessageID: event.MessageID,
AnswerID: event.AnswerID,
},
})
} else {
client.EventManager().DispatchEvent(&events.GuildMessagePollVoteAdd{
GenericGuildMessagePollVote: &events.GenericGuildMessagePollVote{
GenericEvent: genericEvent,
UserID: event.UserID,
ChannelID: event.ChannelID,
MessageID: event.MessageID,
GuildID: *event.GuildID,
AnswerID: event.AnswerID,
},
})
}
}

func gatewayHandlerMessagePollVoteRemove(client bot.Client, sequenceNumber int, shardID int, event gateway.EventMessagePollVoteRemove) {
genericEvent := events.NewGenericEvent(client, sequenceNumber, shardID)

client.EventManager().DispatchEvent(&events.MessagePollVoteRemove{
GenericMessagePoll: &events.GenericMessagePoll{
GenericEvent: events.NewGenericEvent(client, sequenceNumber, shardID),
GenericMessagePollVote: &events.GenericMessagePollVote{
GenericEvent: genericEvent,
UserID: event.UserID,
ChannelID: event.ChannelID,
MessageID: event.MessageID,
GuildID: event.GuildID,
AnswerID: event.AnswerID,
},
})

if event.GuildID == nil {
client.EventManager().DispatchEvent(&events.DMMessagePollVoteRemove{
GenericDMMessagePollVote: &events.GenericDMMessagePollVote{
GenericEvent: genericEvent,
UserID: event.UserID,
ChannelID: event.ChannelID,
MessageID: event.MessageID,
AnswerID: event.AnswerID,
},
})
} else {
client.EventManager().DispatchEvent(&events.GuildMessagePollVoteRemove{
GenericGuildMessagePollVote: &events.GenericGuildMessagePollVote{
GenericEvent: genericEvent,
UserID: event.UserID,
ChannelID: event.ChannelID,
MessageID: event.MessageID,
GuildID: *event.GuildID,
AnswerID: event.AnswerID,
},
})
}
}

0 comments on commit e25946c

Please sign in to comment.