123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- package discordgo
- import (
- "errors"
- "fmt"
- )
- var ErrNilState = errors.New("State not instantiated, please use discordgo.New() or assign Session.State.")
- func NewState() *State {
- return &State{
- Ready: Ready{
- PrivateChannels: []*Channel{},
- Guilds: []*Guild{},
- },
- }
- }
- func (s *State) OnReady(r *Ready) error {
- if s == nil {
- return ErrNilState
- }
- s.Lock()
- defer s.Unlock()
- s.Ready = *r
- return nil
- }
- func (s *State) GuildAdd(guild *Guild) error {
- if s == nil {
- return ErrNilState
- }
- s.Lock()
- defer s.Unlock()
-
- for i, g := range s.Guilds {
- if g.ID == guild.ID {
-
- guild.Members = g.Members
- guild.Presences = g.Presences
- guild.Channels = g.Channels
- guild.VoiceStates = g.VoiceStates
- s.Guilds[i] = guild
- return nil
- }
- }
- s.Guilds = append(s.Guilds, guild)
- return nil
- }
- func (s *State) GuildRemove(guild *Guild) error {
- if s == nil {
- return ErrNilState
- }
- s.Lock()
- defer s.Unlock()
- for i, g := range s.Guilds {
- if g.ID == guild.ID {
- s.Guilds = append(s.Guilds[:i], s.Guilds[i+1:]...)
- return nil
- }
- }
- return errors.New("Guild not found.")
- }
- func (s *State) Guild(guildID string) (*Guild, error) {
- if s == nil {
- return nil, ErrNilState
- }
- s.RLock()
- defer s.RUnlock()
- for _, g := range s.Guilds {
- if g.ID == guildID {
- return g, nil
- }
- }
- return nil, errors.New("Guild not found.")
- }
- func (s *State) MemberAdd(member *Member) error {
- if s == nil {
- return ErrNilState
- }
- guild, err := s.Guild(member.GuildID)
- if err != nil {
- return err
- }
- s.Lock()
- defer s.Unlock()
- for i, m := range guild.Members {
- if m.User.ID == member.User.ID {
- guild.Members[i] = member
- return nil
- }
- }
- guild.Members = append(guild.Members, member)
- return nil
- }
- func (s *State) MemberRemove(member *Member) error {
- if s == nil {
- return ErrNilState
- }
- guild, err := s.Guild(member.GuildID)
- if err != nil {
- return err
- }
- s.Lock()
- defer s.Unlock()
- for i, m := range guild.Members {
- if m.User.ID == member.User.ID {
- guild.Members = append(guild.Members[:i], guild.Members[i+1:]...)
- return nil
- }
- }
- return errors.New("Member not found.")
- }
- func (s *State) Member(guildID, userID string) (*Member, error) {
- if s == nil {
- return nil, ErrNilState
- }
- guild, err := s.Guild(guildID)
- if err != nil {
- return nil, err
- }
- s.RLock()
- defer s.RUnlock()
- for _, m := range guild.Members {
- if m.User.ID == userID {
- return m, nil
- }
- }
- return nil, errors.New("Member not found.")
- }
- func (s *State) ChannelAdd(channel *Channel) error {
- if s == nil {
- return ErrNilState
- }
- if channel.IsPrivate {
- s.Lock()
- defer s.Unlock()
-
- for i, c := range s.PrivateChannels {
- if c.ID == channel.ID {
-
- channel.Messages = c.Messages
- s.PrivateChannels[i] = channel
- return nil
- }
- }
- s.PrivateChannels = append(s.PrivateChannels, channel)
- } else {
- guild, err := s.Guild(channel.GuildID)
- if err != nil {
- return err
- }
- s.Lock()
- defer s.Unlock()
-
- for i, c := range guild.Channels {
- if c.ID == channel.ID {
-
- channel.Messages = c.Messages
- guild.Channels[i] = channel
- return nil
- }
- }
- guild.Channels = append(guild.Channels, channel)
- }
- return nil
- }
- func (s *State) ChannelRemove(channel *Channel) error {
- if s == nil {
- return ErrNilState
- }
- if channel.IsPrivate {
- s.Lock()
- defer s.Unlock()
- for i, c := range s.PrivateChannels {
- if c.ID == channel.ID {
- s.PrivateChannels = append(s.PrivateChannels[:i], s.PrivateChannels[i+1:]...)
- return nil
- }
- }
- } else {
- guild, err := s.Guild(channel.GuildID)
- if err != nil {
- return err
- }
- s.Lock()
- defer s.Unlock()
- for i, c := range guild.Channels {
- if c.ID == channel.ID {
- guild.Channels = append(guild.Channels[:i], guild.Channels[i+1:]...)
- return nil
- }
- }
- }
- return errors.New("Channel not found.")
- }
- func (s *State) GuildChannel(guildID, channelID string) (*Channel, error) {
- if s == nil {
- return nil, ErrNilState
- }
- guild, err := s.Guild(guildID)
- if err != nil {
- return nil, err
- }
- s.RLock()
- defer s.RUnlock()
- for _, c := range guild.Channels {
- if c.ID == channelID {
- return c, nil
- }
- }
- return nil, errors.New("Channel not found.")
- }
- func (s *State) PrivateChannel(channelID string) (*Channel, error) {
- if s == nil {
- return nil, ErrNilState
- }
- s.RLock()
- defer s.RUnlock()
- for _, c := range s.PrivateChannels {
- if c.ID == channelID {
- return c, nil
- }
- }
- return nil, errors.New("Channel not found.")
- }
- func (s *State) Channel(channelID string) (*Channel, error) {
- if s == nil {
- return nil, ErrNilState
- }
- c, err := s.PrivateChannel(channelID)
- if err == nil {
- return c, nil
- }
- for _, g := range s.Guilds {
- c, err := s.GuildChannel(g.ID, channelID)
- if err == nil {
- return c, nil
- }
- }
- return nil, errors.New("Channel not found.")
- }
- func (s *State) Emoji(guildID, emojiID string) (*Emoji, error) {
- if s == nil {
- return nil, ErrNilState
- }
- guild, err := s.Guild(guildID)
- if err != nil {
- return nil, err
- }
- s.RLock()
- defer s.RUnlock()
- for _, e := range guild.Emojis {
- if e.ID == emojiID {
- return e, nil
- }
- }
- return nil, errors.New("Emoji not found.")
- }
- func (s *State) EmojiAdd(guildID string, emoji *Emoji) error {
- if s == nil {
- return ErrNilState
- }
- guild, err := s.Guild(guildID)
- if err != nil {
- return err
- }
- s.Lock()
- defer s.Unlock()
- for i, e := range guild.Emojis {
- if e.ID == emoji.ID {
- guild.Emojis[i] = emoji
- return nil
- }
- }
- guild.Emojis = append(guild.Emojis, emoji)
- return nil
- }
- func (s *State) EmojisAdd(guildID string, emojis []*Emoji) error {
- for _, e := range emojis {
- if err := s.EmojiAdd(guildID, e); err != nil {
- return err
- }
- }
- return nil
- }
- func (s *State) MessageAdd(message *Message) error {
- if s == nil {
- return ErrNilState
- }
- c, err := s.Channel(message.ChannelID)
- if err != nil {
- return err
- }
- s.Lock()
- defer s.Unlock()
-
- for i, m := range c.Messages {
- if m.ID == message.ID {
- c.Messages[i] = message
- return nil
- }
- }
- c.Messages = append(c.Messages, message)
- if len(c.Messages) > s.MaxMessageCount {
- s.Unlock()
- for len(c.Messages) > s.MaxMessageCount {
- err := s.MessageRemove(c.Messages[0])
- if err != nil {
- fmt.Println("message remove error: ", err)
- }
- }
- s.Lock()
- }
- return nil
- }
- func (s *State) MessageRemove(message *Message) error {
- if s == nil {
- return ErrNilState
- }
- c, err := s.Channel(message.ChannelID)
- if err != nil {
- return err
- }
- s.Lock()
- defer s.Unlock()
- for i, m := range c.Messages {
- if m.ID == message.ID {
- c.Messages = append(c.Messages[:i], c.Messages[i+1:]...)
- return nil
- }
- }
- return errors.New("Message not found.")
- }
- func (s *State) Message(channelID, messageID string) (*Message, error) {
- if s == nil {
- return nil, ErrNilState
- }
- c, err := s.Channel(channelID)
- if err != nil {
- return nil, err
- }
- s.RLock()
- defer s.RUnlock()
- for _, m := range c.Messages {
- if m.ID == messageID {
- return m, nil
- }
- }
- return nil, errors.New("Message not found.")
- }
- func (s *State) onInterface(se *Session, i interface{}) (err error) {
- if s == nil {
- return ErrNilState
- }
- if !se.StateEnabled {
- return nil
- }
- switch t := i.(type) {
- case *Ready:
- err = s.OnReady(t)
- case *GuildCreate:
- err = s.GuildAdd(t.Guild)
- case *GuildUpdate:
- err = s.GuildAdd(t.Guild)
- case *GuildDelete:
- err = s.GuildRemove(t.Guild)
- case *GuildMemberAdd:
- err = s.MemberAdd(t.Member)
- case *GuildMemberUpdate:
- err = s.MemberAdd(t.Member)
- case *GuildMemberRemove:
- err = s.MemberRemove(t.Member)
- case *GuildEmojisUpdate:
- err = s.EmojisAdd(t.GuildID, t.Emojis)
- case *ChannelCreate:
- err = s.ChannelAdd(t.Channel)
- case *ChannelUpdate:
- err = s.ChannelAdd(t.Channel)
- case *ChannelDelete:
- err = s.ChannelRemove(t.Channel)
- case *MessageCreate:
- err = s.MessageAdd(t.Message)
- case *MessageUpdate:
- err = s.MessageAdd(t.Message)
- case *MessageDelete:
- err = s.MessageRemove(t.Message)
- }
- return
- }
|