2020-01-18 21:04:12 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2023-09-19 15:23:25 +00:00
|
|
|
"fmt"
|
2020-06-06 20:47:15 +00:00
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v3/state/store"
|
2020-01-18 21:04:12 +00:00
|
|
|
)
|
|
|
|
|
2020-06-19 07:33:22 +00:00
|
|
|
func (s *State) hookSession() {
|
2021-11-03 22:16:02 +00:00
|
|
|
s.Session.AddSyncHandler(func(event interface{}) {
|
2020-06-19 07:33:22 +00:00
|
|
|
// Call the pre-handler before the state handler.
|
|
|
|
if s.PreHandler != nil {
|
|
|
|
s.PreHandler.Call(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the state handler.
|
|
|
|
s.onEvent(event)
|
2020-06-06 20:47:15 +00:00
|
|
|
|
2020-06-19 07:59:44 +00:00
|
|
|
switch event := event.(type) {
|
2020-06-06 20:47:15 +00:00
|
|
|
case *gateway.ReadyEvent:
|
2020-06-19 07:59:44 +00:00
|
|
|
s.Handler.Call(event)
|
|
|
|
s.handleReady(event)
|
2020-06-06 20:47:15 +00:00
|
|
|
case *gateway.GuildCreateEvent:
|
2020-06-19 07:59:44 +00:00
|
|
|
s.Handler.Call(event)
|
|
|
|
s.handleGuildCreate(event)
|
2020-06-06 20:47:15 +00:00
|
|
|
case *gateway.GuildDeleteEvent:
|
2020-06-19 07:59:44 +00:00
|
|
|
s.Handler.Call(event)
|
|
|
|
s.handleGuildDelete(event)
|
2020-06-19 07:33:22 +00:00
|
|
|
|
|
|
|
// https://github.com/discord/discord-api-docs/commit/01665c4
|
|
|
|
case *gateway.MessageCreateEvent:
|
2020-06-19 07:59:44 +00:00
|
|
|
if event.Member != nil {
|
|
|
|
event.Member.User = event.Author
|
2020-06-19 07:33:22 +00:00
|
|
|
}
|
2020-06-19 07:59:44 +00:00
|
|
|
s.Handler.Call(event)
|
|
|
|
|
2020-06-19 07:33:22 +00:00
|
|
|
case *gateway.MessageUpdateEvent:
|
2020-06-19 07:59:44 +00:00
|
|
|
if event.Member != nil {
|
|
|
|
event.Member.User = event.Author
|
2020-06-19 07:33:22 +00:00
|
|
|
}
|
2020-06-19 07:59:44 +00:00
|
|
|
s.Handler.Call(event)
|
|
|
|
|
|
|
|
default:
|
|
|
|
s.Handler.Call(event)
|
2020-06-06 20:47:15 +00:00
|
|
|
}
|
2020-01-18 21:04:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) onEvent(iface interface{}) {
|
|
|
|
switch ev := iface.(type) {
|
|
|
|
case *gateway.ReadyEvent:
|
2020-12-20 02:46:21 +00:00
|
|
|
// Acquire the ready mutex, but since we're only writing the value and
|
|
|
|
// not anything in it, we should be fine.
|
2020-11-14 23:30:18 +00:00
|
|
|
s.readyMu.Lock()
|
|
|
|
s.ready = *ev
|
2020-12-20 02:46:21 +00:00
|
|
|
s.readyMu.Unlock()
|
2020-11-14 23:30:18 +00:00
|
|
|
|
2020-08-18 17:20:48 +00:00
|
|
|
// Reset the store before proceeding.
|
2020-11-30 00:57:58 +00:00
|
|
|
if err := s.Cabinet.Reset(); err != nil {
|
2020-12-20 02:46:21 +00:00
|
|
|
s.stateErr(err, "failed to reset state in Ready")
|
2020-08-18 17:20:48 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
// Handle guilds
|
2020-03-01 02:13:58 +00:00
|
|
|
for i := range ev.Guilds {
|
2020-11-30 00:57:58 +00:00
|
|
|
s.batchLog(storeGuildCreate(s.Cabinet, &ev.Guilds[i]))
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 02:46:21 +00:00
|
|
|
// Handle guild presences
|
2021-11-03 22:16:02 +00:00
|
|
|
for i, presence := range ev.Presences {
|
|
|
|
if err := s.Cabinet.PresenceSet(presence.GuildID, &ev.Presences[i], false); err != nil {
|
2020-12-20 02:46:21 +00:00
|
|
|
s.stateErr(err, "failed to set presence in Ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
// Handle private channels
|
2021-11-03 22:16:02 +00:00
|
|
|
for i := range ev.PrivateChannels {
|
|
|
|
if err := s.Cabinet.ChannelSet(&ev.PrivateChannels[i], false); err != nil {
|
2020-12-20 02:46:21 +00:00
|
|
|
s.stateErr(err, "failed to set channel in Ready")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle user
|
2021-06-03 19:39:49 +00:00
|
|
|
if err := s.Cabinet.MyselfSet(ev.User, false); err != nil {
|
2020-12-20 02:46:21 +00:00
|
|
|
s.stateErr(err, "failed to set self in Ready")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 00:57:58 +00:00
|
|
|
case *gateway.ReadySupplementalEvent:
|
2020-12-19 06:51:26 +00:00
|
|
|
// Handle guilds
|
|
|
|
for _, guild := range ev.Guilds {
|
|
|
|
// Handle guild voice states
|
2021-11-03 22:16:02 +00:00
|
|
|
for i := range guild.VoiceStates {
|
|
|
|
v := &guild.VoiceStates[i]
|
2021-06-03 19:39:49 +00:00
|
|
|
if err := s.Cabinet.VoiceStateSet(guild.ID, v, false); err != nil {
|
2020-12-19 06:51:26 +00:00
|
|
|
s.stateErr(err, "failed to set guild voice state in Ready Supplemental")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
friendPresences := gateway.ConvertSupplementalPresences(ev.MergedPresences.Friends)
|
|
|
|
for i := range friendPresences {
|
|
|
|
if err := s.Cabinet.PresenceSet(0, &friendPresences[i], false); err != nil {
|
2020-12-19 06:51:26 +00:00
|
|
|
s.stateErr(err, "failed to set friend presence in Ready Supplemental")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discord uses weird indexing, so we'll need the Guilds slice.
|
|
|
|
ready := s.Ready()
|
|
|
|
|
|
|
|
for i := 0; i < len(ready.Guilds) && i < len(ev.MergedMembers); i++ {
|
|
|
|
guild := ready.Guilds[i]
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
members := gateway.ConvertSupplementalMembers(ev.MergedMembers[i])
|
|
|
|
for i := range members {
|
|
|
|
if err := s.Cabinet.MemberSet(guild.ID, &members[i], false); err != nil {
|
2020-12-19 06:51:26 +00:00
|
|
|
s.stateErr(err, "failed to set friend presence in Ready Supplemental")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
presences := gateway.ConvertSupplementalPresences(ev.MergedPresences.Guilds[i])
|
|
|
|
for i := range presences {
|
|
|
|
if err := s.Cabinet.PresenceSet(guild.ID, &presences[i], false); err != nil {
|
2020-12-19 06:51:26 +00:00
|
|
|
s.stateErr(err, "failed to set member presence in Ready Supplemental")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-30 00:57:58 +00:00
|
|
|
|
2020-11-02 02:58:03 +00:00
|
|
|
case *gateway.GuildCreateEvent:
|
2020-11-30 00:57:58 +00:00
|
|
|
s.batchLog(storeGuildCreate(s.Cabinet, ev))
|
2020-11-02 02:58:03 +00:00
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
case *gateway.GuildUpdateEvent:
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.GuildSet(&ev.Guild, true); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to update guild in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
case *gateway.GuildDeleteEvent:
|
2020-11-30 00:57:58 +00:00
|
|
|
if err := s.Cabinet.GuildRemove(ev.ID); err != nil && !ev.Unavailable {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to delete guild in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case *gateway.GuildMemberAddEvent:
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.MemberSet(ev.GuildID, &ev.Member, false); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to add a member in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
case *gateway.GuildMemberUpdateEvent:
|
2020-11-30 00:57:58 +00:00
|
|
|
m, err := s.Cabinet.Member(ev.GuildID, ev.User.ID)
|
2020-01-18 21:04:12 +00:00
|
|
|
if err != nil {
|
|
|
|
// We can't do much here.
|
|
|
|
m = &discord.Member{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update available fields from ev into m
|
2021-09-28 20:19:04 +00:00
|
|
|
ev.UpdateMember(m)
|
2020-01-18 21:04:12 +00:00
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.MemberSet(ev.GuildID, m, true); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to update a member in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
case *gateway.GuildMemberRemoveEvent:
|
2020-11-30 00:57:58 +00:00
|
|
|
if err := s.Cabinet.MemberRemove(ev.GuildID, ev.User.ID); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to remove a member in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case *gateway.GuildMembersChunkEvent:
|
2021-11-03 22:16:02 +00:00
|
|
|
for i := range ev.Members {
|
|
|
|
if err := s.Cabinet.MemberSet(ev.GuildID, &ev.Members[i], false); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to add a member from chunk in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
for i := range ev.Presences {
|
|
|
|
if err := s.Cabinet.PresenceSet(ev.GuildID, &ev.Presences[i], false); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to add a presence from chunk in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *gateway.GuildRoleCreateEvent:
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.RoleSet(ev.GuildID, &ev.Role, false); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to add a role in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
case *gateway.GuildRoleUpdateEvent:
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.RoleSet(ev.GuildID, &ev.Role, true); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to update a role in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
case *gateway.GuildRoleDeleteEvent:
|
2020-11-30 00:57:58 +00:00
|
|
|
if err := s.Cabinet.RoleRemove(ev.GuildID, ev.RoleID); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to remove a role in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case *gateway.GuildEmojisUpdateEvent:
|
2021-06-03 19:39:49 +00:00
|
|
|
if err := s.Cabinet.EmojiSet(ev.GuildID, ev.Emojis, true); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to update emojis in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case *gateway.ChannelCreateEvent:
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.ChannelSet(&ev.Channel, false); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to create a channel in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
case *gateway.ChannelUpdateEvent:
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.ChannelSet(&ev.Channel, true); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to update a channel in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
case *gateway.ChannelDeleteEvent:
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.ChannelRemove(&ev.Channel); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to remove a channel in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 23:14:27 +00:00
|
|
|
case *gateway.ChannelPinsUpdateEvent:
|
|
|
|
// not tracked.
|
2020-01-18 21:04:12 +00:00
|
|
|
|
2022-04-01 11:40:15 +00:00
|
|
|
case *gateway.ThreadListSyncEvent:
|
|
|
|
for i := range ev.Threads {
|
|
|
|
if err := s.Cabinet.ChannelSet(&ev.Threads[i], true); err != nil {
|
|
|
|
s.stateErr(err, "failed to set a thread in state sync")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *gateway.ThreadCreateEvent:
|
|
|
|
if err := s.Cabinet.ChannelSet(&ev.Channel, false); err != nil {
|
|
|
|
s.stateErr(err, "failed to create a thread in state")
|
|
|
|
}
|
|
|
|
|
|
|
|
case *gateway.ThreadUpdateEvent:
|
|
|
|
if err := s.Cabinet.ChannelSet(&ev.Channel, true); err != nil {
|
|
|
|
s.stateErr(err, "failed to update a thread in state")
|
|
|
|
}
|
|
|
|
|
|
|
|
case *gateway.ThreadDeleteEvent:
|
|
|
|
if ch, err := s.Cabinet.Channel(ev.ID); err == nil {
|
|
|
|
if err := s.Cabinet.ChannelRemove(ch); err != nil {
|
|
|
|
s.stateErr(err, "failed to delete a thread in state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
case *gateway.MessageCreateEvent:
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.MessageSet(&ev.Message, false); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to add a message in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
case *gateway.MessageUpdateEvent:
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.MessageSet(&ev.Message, true); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to update a message in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
case *gateway.MessageDeleteEvent:
|
2020-11-30 00:57:58 +00:00
|
|
|
if err := s.Cabinet.MessageRemove(ev.ChannelID, ev.ID); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to delete a message in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
case *gateway.MessageDeleteBulkEvent:
|
|
|
|
for _, id := range ev.IDs {
|
2020-11-30 00:57:58 +00:00
|
|
|
if err := s.Cabinet.MessageRemove(ev.ChannelID, id); err != nil {
|
2020-07-29 23:29:01 +00:00
|
|
|
s.stateErr(err, "failed to delete bulk messages in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-04-12 23:14:27 +00:00
|
|
|
case *gateway.MessageReactionAddEvent:
|
2024-02-14 11:26:30 +00:00
|
|
|
var me bool
|
|
|
|
if u, _ := s.Cabinet.Me(); u != nil {
|
|
|
|
me = ev.UserID == u.ID
|
|
|
|
}
|
|
|
|
|
2020-04-12 23:14:27 +00:00
|
|
|
s.editMessage(ev.ChannelID, ev.MessageID, func(m *discord.Message) bool {
|
|
|
|
if i := findReaction(m.Reactions, ev.Emoji); i > -1 {
|
2021-11-03 22:16:02 +00:00
|
|
|
// Copy the reactions slice so it's not racy.
|
|
|
|
m.Reactions = append([]discord.Reaction(nil), m.Reactions...)
|
2020-04-12 23:14:27 +00:00
|
|
|
m.Reactions[i].Count++
|
2024-02-14 11:26:30 +00:00
|
|
|
m.Reactions[i].Me = m.Reactions[i].Me || me
|
2020-04-12 23:14:27 +00:00
|
|
|
} else {
|
2021-11-03 22:16:02 +00:00
|
|
|
old := m.Reactions
|
|
|
|
m.Reactions = make([]discord.Reaction, 0, len(old)+1)
|
|
|
|
m.Reactions = append(m.Reactions, old...)
|
2020-04-12 23:14:27 +00:00
|
|
|
m.Reactions = append(m.Reactions, discord.Reaction{
|
|
|
|
Count: 1,
|
2020-04-13 04:25:22 +00:00
|
|
|
Me: me,
|
2020-04-12 23:14:27 +00:00
|
|
|
Emoji: ev.Emoji,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-04-12 23:14:27 +00:00
|
|
|
case *gateway.MessageReactionRemoveEvent:
|
|
|
|
s.editMessage(ev.ChannelID, ev.MessageID, func(m *discord.Message) bool {
|
|
|
|
var i = findReaction(m.Reactions, ev.Emoji)
|
|
|
|
if i < 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
r := &m.Reactions[i]
|
2024-02-14 11:26:30 +00:00
|
|
|
// If the count is 0:
|
|
|
|
if (r.Count - 1) < 1 {
|
2021-11-03 22:16:02 +00:00
|
|
|
old := m.Reactions
|
|
|
|
m.Reactions = make([]discord.Reaction, len(m.Reactions)-1)
|
|
|
|
copy(m.Reactions[0:], old[:i])
|
|
|
|
copy(m.Reactions[i:], old[i+1:])
|
2024-02-14 11:26:30 +00:00
|
|
|
} else {
|
2021-11-03 22:16:02 +00:00
|
|
|
r.Count--
|
2022-01-18 00:35:49 +00:00
|
|
|
if r.Me { // If reaction removal is the user's
|
|
|
|
u, err := s.Cabinet.Me()
|
|
|
|
if err == nil && ev.UserID == u.ID {
|
|
|
|
r.Me = false
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 23:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-04-12 23:14:27 +00:00
|
|
|
case *gateway.MessageReactionRemoveAllEvent:
|
|
|
|
s.editMessage(ev.ChannelID, ev.MessageID, func(m *discord.Message) bool {
|
|
|
|
m.Reactions = nil
|
|
|
|
return true
|
|
|
|
})
|
2020-04-13 04:25:22 +00:00
|
|
|
|
2020-10-29 02:47:22 +00:00
|
|
|
case *gateway.MessageReactionRemoveEmojiEvent:
|
2020-04-12 23:14:27 +00:00
|
|
|
s.editMessage(ev.ChannelID, ev.MessageID, func(m *discord.Message) bool {
|
|
|
|
var i = findReaction(m.Reactions, ev.Emoji)
|
|
|
|
if i < 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.Reactions = append(m.Reactions[:i], m.Reactions[i+1:]...)
|
|
|
|
return true
|
|
|
|
})
|
2020-01-18 21:04:12 +00:00
|
|
|
|
|
|
|
case *gateway.PresenceUpdateEvent:
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.PresenceSet(ev.GuildID, &ev.Presence, true); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to update presence in state")
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
2020-02-24 04:11:33 +00:00
|
|
|
|
|
|
|
case *gateway.PresencesReplaceEvent:
|
2020-07-28 19:00:01 +00:00
|
|
|
for _, p := range *ev {
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.PresenceSet(p.GuildID, &p.Presence, true); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to update presence in state")
|
2020-02-24 04:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-25 05:50:13 +00:00
|
|
|
|
2020-05-11 23:57:40 +00:00
|
|
|
case *gateway.SessionsReplaceEvent:
|
2020-11-30 00:57:58 +00:00
|
|
|
// TODO
|
2020-05-11 23:57:40 +00:00
|
|
|
|
2020-02-25 05:50:13 +00:00
|
|
|
case *gateway.UserGuildSettingsUpdateEvent:
|
2020-11-30 00:57:58 +00:00
|
|
|
// TODO
|
2020-02-25 05:50:13 +00:00
|
|
|
|
|
|
|
case *gateway.UserSettingsUpdateEvent:
|
2020-11-14 23:30:18 +00:00
|
|
|
s.readyMu.Lock()
|
2020-11-30 00:57:58 +00:00
|
|
|
s.ready.UserSettings = &ev.UserSettings
|
2020-11-14 23:30:18 +00:00
|
|
|
s.readyMu.Unlock()
|
2020-02-25 05:50:13 +00:00
|
|
|
|
|
|
|
case *gateway.UserNoteUpdateEvent:
|
2020-11-30 00:57:58 +00:00
|
|
|
// TODO
|
2020-02-25 05:50:13 +00:00
|
|
|
|
|
|
|
case *gateway.UserUpdateEvent:
|
2021-06-03 19:39:49 +00:00
|
|
|
if err := s.Cabinet.MyselfSet(ev.User, true); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to update myself from USER_UPDATE")
|
2020-02-25 05:50:13 +00:00
|
|
|
}
|
2020-04-19 02:22:49 +00:00
|
|
|
|
|
|
|
case *gateway.VoiceStateUpdateEvent:
|
2020-05-12 03:06:28 +00:00
|
|
|
vs := &ev.VoiceState
|
2021-06-08 09:53:54 +00:00
|
|
|
if !vs.ChannelID.IsValid() {
|
2020-11-30 00:57:58 +00:00
|
|
|
if err := s.Cabinet.VoiceStateRemove(vs.GuildID, vs.UserID); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to remove voice state from state")
|
2020-04-19 02:22:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := s.Cabinet.VoiceStateSet(vs.GuildID, vs, true); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to update voice state in state")
|
2020-04-19 02:22:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 09:06:47 +00:00
|
|
|
func (s *State) stateErr(err error, wrap string) {
|
2023-09-19 15:23:25 +00:00
|
|
|
s.StateLog(fmt.Errorf("%s: %w", wrap, err))
|
2020-01-18 21:04:12 +00:00
|
|
|
}
|
2021-11-03 22:16:02 +00:00
|
|
|
|
2020-11-02 02:58:03 +00:00
|
|
|
func (s *State) batchLog(errors []error) {
|
2020-04-06 19:16:32 +00:00
|
|
|
for _, err := range errors {
|
|
|
|
s.StateLog(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) editMessage(ch discord.ChannelID, msg discord.MessageID, fn func(m *discord.Message) bool) {
|
2020-11-30 00:57:58 +00:00
|
|
|
m, err := s.Cabinet.Message(ch, msg)
|
2020-04-12 23:14:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-11-03 22:16:02 +00:00
|
|
|
|
|
|
|
// Copy the messages.
|
|
|
|
cpy := *m
|
|
|
|
m = &cpy
|
|
|
|
|
2020-04-12 23:14:27 +00:00
|
|
|
if !fn(m) {
|
|
|
|
return
|
|
|
|
}
|
2021-11-03 22:16:02 +00:00
|
|
|
|
|
|
|
if err := s.Cabinet.MessageSet(m, true); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.stateErr(err, "failed to save message in reaction add")
|
2020-04-12 23:14:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func findReaction(rs []discord.Reaction, emoji discord.Emoji) int {
|
|
|
|
for i := range rs {
|
|
|
|
if rs[i].Emoji.ID == emoji.ID && rs[i].Emoji.Name == emoji.Name {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
func storeGuildCreate(cab *store.Cabinet, guild *gateway.GuildCreateEvent) []error {
|
2020-04-06 19:16:32 +00:00
|
|
|
if guild.Unavailable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-06 20:47:15 +00:00
|
|
|
stack, errs := newErrorStack()
|
2020-04-06 19:16:32 +00:00
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := cab.GuildSet(&guild.Guild, false); err != nil {
|
2020-06-06 20:47:15 +00:00
|
|
|
errs(err, "failed to set guild in Ready")
|
2020-04-06 19:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle guild emojis
|
2021-08-20 03:58:29 +00:00
|
|
|
if len(guild.Emojis) > 0 {
|
2021-06-03 19:39:49 +00:00
|
|
|
if err := cab.EmojiSet(guild.ID, guild.Emojis, false); err != nil {
|
2020-06-06 20:47:15 +00:00
|
|
|
errs(err, "failed to set guild emojis")
|
2020-04-06 19:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle guild member
|
2021-11-03 22:16:02 +00:00
|
|
|
for i := range guild.Members {
|
|
|
|
if err := cab.MemberSet(guild.ID, &guild.Members[i], false); err != nil {
|
2020-06-06 20:47:15 +00:00
|
|
|
errs(err, "failed to set guild member in Ready")
|
2020-04-06 19:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle guild channels
|
2020-07-28 19:00:01 +00:00
|
|
|
for _, ch := range guild.Channels {
|
2020-05-18 06:11:14 +00:00
|
|
|
// I HATE Discord.
|
2021-11-03 22:16:02 +00:00
|
|
|
ch := ch
|
2020-05-18 06:11:14 +00:00
|
|
|
ch.GuildID = guild.ID
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := cab.ChannelSet(&ch, false); err != nil {
|
2020-06-06 20:47:15 +00:00
|
|
|
errs(err, "failed to set guild channel in Ready")
|
2020-04-06 19:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 03:58:29 +00:00
|
|
|
// Handle threads.
|
|
|
|
for _, ch := range guild.Threads {
|
2021-11-03 22:16:02 +00:00
|
|
|
ch := ch
|
2021-08-20 03:58:29 +00:00
|
|
|
ch.GuildID = guild.ID
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := cab.ChannelSet(&ch, false); err != nil {
|
2021-08-20 03:58:29 +00:00
|
|
|
errs(err, "failed to set guild thread in Ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:16:32 +00:00
|
|
|
// Handle guild presences
|
2020-07-28 19:00:01 +00:00
|
|
|
for _, p := range guild.Presences {
|
2021-11-03 22:16:02 +00:00
|
|
|
p := p
|
2021-08-20 03:58:29 +00:00
|
|
|
p.GuildID = guild.ID
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := cab.PresenceSet(guild.ID, &p, false); err != nil {
|
2020-06-06 20:47:15 +00:00
|
|
|
errs(err, "failed to set guild presence in Ready")
|
2020-04-06 19:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 02:34:25 +00:00
|
|
|
// Handle guild voice states
|
2020-07-28 19:00:01 +00:00
|
|
|
for _, v := range guild.VoiceStates {
|
2021-11-03 22:16:02 +00:00
|
|
|
v := v
|
2021-08-20 03:58:29 +00:00
|
|
|
v.GuildID = guild.ID
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
if err := cab.VoiceStateSet(guild.ID, &v, false); err != nil {
|
2020-06-06 20:47:15 +00:00
|
|
|
errs(err, "failed to set guild voice state in Ready")
|
2020-04-19 02:34:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 11:40:15 +00:00
|
|
|
// Handle guild roles
|
|
|
|
for _, r := range guild.Roles {
|
|
|
|
r := r
|
|
|
|
|
|
|
|
if err := cab.RoleSet(guild.ID, &r, false); err != nil {
|
|
|
|
errs(err, "failed to set role in Ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:16:32 +00:00
|
|
|
return *stack
|
|
|
|
}
|
|
|
|
|
|
|
|
func newErrorStack() (*[]error, func(error, string)) {
|
|
|
|
var errs = new([]error)
|
|
|
|
return errs, func(err error, wrap string) {
|
2023-09-19 15:23:25 +00:00
|
|
|
*errs = append(*errs, fmt.Errorf("%s: %w", wrap, err))
|
2020-04-06 19:16:32 +00:00
|
|
|
}
|
|
|
|
}
|