1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-01-02 18:26:41 +00:00

Gateway: Added extra events

This commit is contained in:
diamondburned (Forefront) 2020-02-15 21:29:25 -08:00
parent 0de1e579f3
commit 4e9bd53b0f
6 changed files with 56 additions and 33 deletions

View file

@ -28,6 +28,15 @@ type (
ChannelID discord.Snowflake `json:"channel_id,omitempty"`
LastPin discord.Timestamp `json:"timestamp,omitempty"`
}
ChannelUnreadUpdateEvent struct {
GuildID discord.Snowflake `json:"guild_id"`
ChannelUnreadUpdates []struct {
ID discord.Snowflake `json:"id"`
LastMessageID discord.Snowflake `json:"last_message_id"`
}
}
)
// https://discordapp.com/developers/docs/topics/gateway#guilds
@ -154,13 +163,19 @@ type (
MessageReactionRemoveAllEvent struct {
ChannelID discord.Snowflake `json:"channel_id"`
}
MessageAckEvent struct {
MessageID discord.Snowflake `json:"message_id"`
ChannelID discord.Snowflake `json:"channel_id"`
}
)
// https://discordapp.com/developers/docs/topics/gateway#presence
type (
// Clients may only update their game status 5 times per 20 seconds.
PresenceUpdateEvent discord.Presence
TypingStartEvent struct {
PresenceUpdateEvent discord.Presence
PresencesReplaceEvent []discord.Presence
TypingStartEvent struct {
ChannelID discord.Snowflake `json:"channel_id"`
UserID discord.Snowflake `json:"user_id"`
Timestamp discord.UnixTimestamp `json:"timestamp"`
@ -193,4 +208,13 @@ type (
type (
UserGuildSettingsUpdateEvent UserGuildSettings
UserSettingsUpdateEvent UserSettings
UserNoteUpdateEvent struct {
ID discord.Snowflake `json:"id"`
Note string `json:"note"`
}
)
type (
RelationshipAdd Relationship
RelationshipRemove Relationship
)

View file

@ -13,6 +13,9 @@ var EventCreator = map[string]func() Event{
"CHANNEL_UPDATE": func() Event { return new(ChannelUpdateEvent) },
"CHANNEL_DELETE": func() Event { return new(ChannelDeleteEvent) },
"CHANNEL_PINS_UPDATE": func() Event { return new(ChannelPinsUpdateEvent) },
"CHANNEL_UNREAD_UPDATE": func() Event {
return new(ChannelUnreadUpdateEvent)
},
"GUILD_CREATE": func() Event { return new(GuildCreateEvent) },
"GUILD_UPDATE": func() Event { return new(GuildUpdateEvent) },
@ -50,12 +53,28 @@ var EventCreator = map[string]func() Event{
return new(MessageReactionRemoveAllEvent)
},
"PRESENCE_UPDATE": func() Event { return new(PresenceUpdateEvent) },
"TYPING_START": func() Event { return new(TypingStartEvent) },
"USER_UPDATE": func() Event { return new(UserUpdateEvent) },
"MESSAGE_ACK": func() Event { return new(MessageAckEvent) },
"PRESENCE_UPDATE": func() Event { return new(PresenceUpdateEvent) },
"PRESENCES_REPLACE": func() Event { return new(PresencesReplaceEvent) },
"TYPING_START": func() Event { return new(TypingStartEvent) },
"VOICE_STATE_UPDATE": func() Event { return new(VoiceStateUpdateEvent) },
"VOICE_SERVER_UPDATE": func() Event { return new(VoiceServerUpdateEvent) },
"WEBHOOKS_UPDATE": func() Event { return new(WebhooksUpdateEvent) },
"USER_UPDATE": func() Event {
return new(UserUpdateEvent)
},
"USER_SETTINGS_UPDATE": func() Event {
return new(UserSettingsUpdateEvent)
},
"USER_GUILD_SETTINGS_UPDATE": func() Event {
return new(UserGuildSettingsUpdateEvent)
},
"USER_NOTE_UPDATE": func() Event {
return new(UserNoteUpdateEvent)
},
}

View file

@ -147,7 +147,10 @@ func HandleOP(g *Gateway, op *OP) error {
// Check if we know the event
fn, ok := EventCreator[op.EventName]
if !ok {
return errors.New("Unknown event: " + op.EventName)
return fmt.Errorf(
"Unknown event %s: %s",
op.EventName, string(op.Data),
)
}
// Make a new pointer to the event

View file

@ -4,8 +4,6 @@
package session
import (
"log"
"github.com/diamondburned/arikawa/api"
"github.com/diamondburned/arikawa/gateway"
"github.com/diamondburned/arikawa/handler"
@ -20,9 +18,6 @@ type Session struct {
*api.Client
Gateway *gateway.Gateway
// ErrorLog logs errors, including Gateway errors.
ErrorLog func(err error) // default to log.Println
// Command handler with inherited methods.
*handler.Handler
@ -39,20 +34,12 @@ func New(token string) (*Session, error) {
s.Handler = handler.New()
s.Client = api.NewClient(token)
// Default logger
s.ErrorLog = func(err error) {
log.Println("Arikawa/session error:", err)
}
// Open a gateway
g, err := gateway.NewGateway(token)
if err != nil {
return nil, errors.Wrap(err, "Failed to connect to Gateway")
}
s.Gateway = g
s.Gateway.ErrorLog = func(err error) {
s.ErrorLog(err)
}
return s, nil
}
@ -88,20 +75,12 @@ func Login(email, password, mfa string) (*Session, error) {
}
func NewWithGateway(gw *gateway.Gateway) *Session {
s := &Session{
return &Session{
Gateway: gw,
// Nab off gateway's token
Client: api.NewClient(gw.Identifier.Token),
ErrorLog: func(err error) {
log.Println("Arikawa/session error:", err)
},
Client: api.NewClient(gw.Identifier.Token),
Handler: handler.New(),
}
gw.ErrorLog = func(err error) {
s.ErrorLog(err)
}
return s
}
func (s *Session) Open() error {

View file

@ -51,8 +51,6 @@ func NewFromSession(s *session.Session, store Store) (*State, error) {
StateLog: func(err error) {},
}
s.ErrorLog = state.ErrorLog
return state, state.hookSession()
}

View file

@ -187,5 +187,5 @@ func (s *State) onEvent(iface interface{}) {
}
func (s *State) stateErr(err error, wrap string) {
s.ErrorLog(errors.Wrap(err, wrap))
s.StateLog(errors.Wrap(err, wrap))
}