mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-01-20 11:37:56 +00:00
Added state example
This commit is contained in:
parent
b11b3e1a42
commit
5b0687bcd3
55
_example/undeleter/main.go
Normal file
55
_example/undeleter/main.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/diamondburned/arikawa/gateway"
|
||||
"github.com/diamondburned/arikawa/handler"
|
||||
"github.com/diamondburned/arikawa/state"
|
||||
)
|
||||
|
||||
// To run, do `BOT_TOKEN="TOKEN HERE" go run .`
|
||||
|
||||
func main() {
|
||||
var token = os.Getenv("BOT_TOKEN")
|
||||
if token == "" {
|
||||
log.Fatalln("No $BOT_TOKEN given.")
|
||||
}
|
||||
|
||||
s, err := state.New("Bot " + token)
|
||||
if err != nil {
|
||||
log.Fatalln("Session failed:", err)
|
||||
}
|
||||
|
||||
// Make a pre-handler
|
||||
s.PreHandler = handler.New()
|
||||
s.PreHandler.Synchronous = true
|
||||
s.PreHandler.AddHandler(func(c *gateway.MessageDeleteEvent) {
|
||||
// Grab from the state
|
||||
m, err := s.Message(c.ChannelID, c.ID)
|
||||
if err != nil {
|
||||
log.Println("Not found:", c.ID)
|
||||
} else {
|
||||
log.Println("Deleted:", m.Author.Username+":", m.Content)
|
||||
}
|
||||
})
|
||||
|
||||
if err := s.Open(); err != nil {
|
||||
log.Fatalln("Failed to connect:", err)
|
||||
}
|
||||
|
||||
defer s.Close()
|
||||
|
||||
u, err := s.Self()
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to get myself:", err)
|
||||
}
|
||||
|
||||
log.Println("Started as", u.Username)
|
||||
|
||||
// Wait is optional.
|
||||
if err := s.Wait(); err != nil {
|
||||
log.Fatalln("Fatal error:", err)
|
||||
}
|
||||
}
|
|
@ -104,8 +104,8 @@ type UpdateVoiceStateData struct {
|
|||
|
||||
type UpdateStatusData struct {
|
||||
Since discord.Milliseconds `json:"since,omitempty"` // 0 if not idle
|
||||
Game *Activity `json:"game,omitempty"` // nullable
|
||||
Game *discord.Activity `json:"game,omitempty"` // nullable
|
||||
|
||||
Status Status `json:"status"`
|
||||
AFK bool `json:"afk"`
|
||||
Status discord.Status `json:"status"`
|
||||
AFK bool `json:"afk"`
|
||||
}
|
||||
|
|
|
@ -20,6 +20,8 @@ type State struct {
|
|||
*session.Session
|
||||
Store
|
||||
|
||||
// *: State doesn't actually keep track of pinned messages.
|
||||
|
||||
// Ready is not updated by the state.
|
||||
Ready gateway.ReadyEvent
|
||||
|
||||
|
@ -33,8 +35,6 @@ type State struct {
|
|||
// It's recommended to set Synchronous to true if you mutate the events.
|
||||
PreHandler *handler.Handler // default nil
|
||||
|
||||
// *: State doesn't actually keep track of pinned messages.
|
||||
|
||||
unhooker func()
|
||||
}
|
||||
|
||||
|
|
|
@ -46,6 +46,7 @@ func NewDefaultStore(opts *DefaultStoreOptions) *DefaultStore {
|
|||
guilds: map[discord.Snowflake]*discord.Guild{},
|
||||
|
||||
channels: map[discord.Snowflake][]discord.Channel{},
|
||||
members: map[discord.Snowflake][]discord.Member{},
|
||||
presences: map[discord.Snowflake][]discord.Presence{},
|
||||
messages: map[discord.Snowflake][]discord.Message{},
|
||||
}
|
||||
|
@ -143,10 +144,7 @@ func (s *DefaultStore) ChannelSet(channel *discord.Channel) error {
|
|||
s.privates[channel.ID] = channel
|
||||
|
||||
default:
|
||||
chs, ok := s.channels[channel.GuildID]
|
||||
if !ok {
|
||||
return ErrStoreNotFound
|
||||
}
|
||||
chs := s.channels[channel.GuildID]
|
||||
|
||||
for i, ch := range chs {
|
||||
if ch.ID == channel.ID {
|
||||
|
@ -344,10 +342,7 @@ func (s *DefaultStore) MemberSet(
|
|||
s.mut.Lock()
|
||||
defer s.mut.Unlock()
|
||||
|
||||
ms, ok := s.members[guildID]
|
||||
if !ok {
|
||||
return ErrStoreNotFound
|
||||
}
|
||||
ms := s.members[guildID]
|
||||
|
||||
// Try and see if this member is already in the slice
|
||||
for i, m := range ms {
|
||||
|
@ -446,6 +441,7 @@ func (s *DefaultStore) MessageSet(message *discord.Message) error {
|
|||
ms = ms[len(ms)-int(s.MaxMessages):]
|
||||
}
|
||||
|
||||
s.messages[message.ChannelID] = ms
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -513,10 +509,7 @@ func (s *DefaultStore) PresenceSet(
|
|||
s.mut.Lock()
|
||||
defer s.mut.Unlock()
|
||||
|
||||
ps, ok := s.presences[guildID]
|
||||
if !ok {
|
||||
return ErrStoreNotFound
|
||||
}
|
||||
ps := s.presences[guildID]
|
||||
|
||||
for i, p := range ps {
|
||||
if p.User.ID == presence.User.ID {
|
||||
|
|
Loading…
Reference in a new issue