state.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // Discordgo - Discord bindings for Go
  2. // Available at https://github.com/bwmarrin/discordgo
  3. // Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>. All rights reserved.
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file.
  6. // This file contains code related to state tracking. If enabled, state
  7. // tracking will capture the initial READY packet and many other websocket
  8. // events and maintain an in-memory state of of guilds, channels, users, and
  9. // so forth. This information can be accessed through the Session.State struct.
  10. package discordgo
  11. import "errors"
  12. // ErrNilState is returned when the state is nil.
  13. var ErrNilState = errors.New("State not instantiated, please use discordgo.New() or assign Session.State.")
  14. // NewState creates an empty state.
  15. func NewState() *State {
  16. return &State{
  17. Ready: Ready{
  18. PrivateChannels: []*Channel{},
  19. Guilds: []*Guild{},
  20. },
  21. guildMap: make(map[string]*Guild),
  22. channelMap: make(map[string]*Channel),
  23. }
  24. }
  25. // OnReady takes a Ready event and updates all internal state.
  26. func (s *State) OnReady(r *Ready) error {
  27. if s == nil {
  28. return ErrNilState
  29. }
  30. s.Lock()
  31. defer s.Unlock()
  32. s.Ready = *r
  33. for _, g := range s.Guilds {
  34. for _, c := range g.Channels {
  35. c.GuildID = g.ID
  36. }
  37. s.guildMap[g.ID] = g
  38. }
  39. return nil
  40. }
  41. // GuildAdd adds a guild to the current world state, or
  42. // updates it if it already exists.
  43. func (s *State) GuildAdd(guild *Guild) error {
  44. if s == nil {
  45. return ErrNilState
  46. }
  47. // Otherwise, update the channels to point to the right guild
  48. for _, c := range guild.Channels {
  49. c.GuildID = guild.ID
  50. }
  51. if g, err := s.Guild(guild.ID); err == nil {
  52. s.Lock()
  53. defer s.Unlock()
  54. if !g.Unavailable {
  55. guild.Members = g.Members
  56. guild.Presences = g.Presences
  57. guild.Channels = g.Channels
  58. guild.VoiceStates = g.VoiceStates
  59. }
  60. *g = *guild
  61. return nil
  62. }
  63. s.Lock()
  64. defer s.Unlock()
  65. s.Guilds = append(s.Guilds, guild)
  66. s.guildMap[guild.ID] = guild
  67. return nil
  68. }
  69. // GuildRemove removes a guild from current world state.
  70. func (s *State) GuildRemove(guild *Guild) error {
  71. if s == nil {
  72. return ErrNilState
  73. }
  74. _, err := s.Guild(guild.ID)
  75. if err != nil {
  76. return err
  77. }
  78. s.Lock()
  79. defer s.Unlock()
  80. for i, g := range s.Guilds {
  81. if g.ID == guild.ID {
  82. s.Guilds = append(s.Guilds[:i], s.Guilds[i+1:]...)
  83. return nil
  84. }
  85. }
  86. delete(s.guildMap, guild.ID)
  87. return nil
  88. }
  89. // Guild gets a guild by ID.
  90. // Useful for querying if @me is in a guild:
  91. // _, err := discordgo.Session.State.Guild(guildID)
  92. // isInGuild := err == nil
  93. func (s *State) Guild(guildID string) (*Guild, error) {
  94. if s == nil {
  95. return nil, ErrNilState
  96. }
  97. s.RLock()
  98. defer s.RUnlock()
  99. if g, ok := s.guildMap[guildID]; ok {
  100. return g, nil
  101. }
  102. return nil, errors.New("Guild not found.")
  103. }
  104. // TODO: Consider moving Guild state update methods onto *Guild.
  105. // MemberAdd adds a member to the current world state, or
  106. // updates it if it already exists.
  107. func (s *State) MemberAdd(member *Member) error {
  108. if s == nil {
  109. return ErrNilState
  110. }
  111. guild, err := s.Guild(member.GuildID)
  112. if err != nil {
  113. return err
  114. }
  115. s.Lock()
  116. defer s.Unlock()
  117. for i, m := range guild.Members {
  118. if m.User.ID == member.User.ID {
  119. guild.Members[i] = member
  120. return nil
  121. }
  122. }
  123. guild.Members = append(guild.Members, member)
  124. return nil
  125. }
  126. // MemberRemove removes a member from current world state.
  127. func (s *State) MemberRemove(member *Member) error {
  128. if s == nil {
  129. return ErrNilState
  130. }
  131. guild, err := s.Guild(member.GuildID)
  132. if err != nil {
  133. return err
  134. }
  135. s.Lock()
  136. defer s.Unlock()
  137. for i, m := range guild.Members {
  138. if m.User.ID == member.User.ID {
  139. guild.Members = append(guild.Members[:i], guild.Members[i+1:]...)
  140. return nil
  141. }
  142. }
  143. return errors.New("Member not found.")
  144. }
  145. // Member gets a member by ID from a guild.
  146. func (s *State) Member(guildID, userID string) (*Member, error) {
  147. if s == nil {
  148. return nil, ErrNilState
  149. }
  150. guild, err := s.Guild(guildID)
  151. if err != nil {
  152. return nil, err
  153. }
  154. s.RLock()
  155. defer s.RUnlock()
  156. for _, m := range guild.Members {
  157. if m.User.ID == userID {
  158. return m, nil
  159. }
  160. }
  161. return nil, errors.New("Member not found.")
  162. }
  163. // ChannelAdd adds a guild to the current world state, or
  164. // updates it if it already exists.
  165. // Channels may exist either as PrivateChannels or inside
  166. // a guild.
  167. func (s *State) ChannelAdd(channel *Channel) error {
  168. if s == nil {
  169. return ErrNilState
  170. }
  171. if c, err := s.Channel(channel.ID); err == nil {
  172. s.Lock()
  173. defer s.Unlock()
  174. channel.Messages = c.Messages
  175. channel.PermissionOverwrites = c.PermissionOverwrites
  176. *c = *channel
  177. return nil
  178. }
  179. s.Lock()
  180. defer s.Unlock()
  181. if channel.IsPrivate {
  182. s.PrivateChannels = append(s.PrivateChannels, channel)
  183. } else {
  184. guild, err := s.Guild(channel.GuildID)
  185. if err != nil {
  186. return err
  187. }
  188. guild.Channels = append(guild.Channels, channel)
  189. }
  190. s.channelMap[channel.ID] = channel
  191. return nil
  192. }
  193. // ChannelRemove removes a channel from current world state.
  194. func (s *State) ChannelRemove(channel *Channel) error {
  195. if s == nil {
  196. return ErrNilState
  197. }
  198. _, err := s.Channel(channel.ID)
  199. if err != nil {
  200. return err
  201. }
  202. if channel.IsPrivate {
  203. s.Lock()
  204. defer s.Unlock()
  205. for i, c := range s.PrivateChannels {
  206. if c.ID == channel.ID {
  207. s.PrivateChannels = append(s.PrivateChannels[:i], s.PrivateChannels[i+1:]...)
  208. return nil
  209. }
  210. }
  211. } else {
  212. guild, err := s.Guild(channel.GuildID)
  213. if err != nil {
  214. return err
  215. }
  216. s.Lock()
  217. defer s.Unlock()
  218. for i, c := range guild.Channels {
  219. if c.ID == channel.ID {
  220. guild.Channels = append(guild.Channels[:i], guild.Channels[i+1:]...)
  221. return nil
  222. }
  223. }
  224. }
  225. delete(s.channelMap, channel.ID)
  226. return nil
  227. }
  228. // GuildChannel gets a channel by ID from a guild.
  229. // This method is Deprecated, use Channel(channelID)
  230. func (s *State) GuildChannel(guildID, channelID string) (*Channel, error) {
  231. return s.Channel(channelID)
  232. }
  233. // PrivateChannel gets a private channel by ID.
  234. // This method is Deprecated, use Channel(channelID)
  235. func (s *State) PrivateChannel(channelID string) (*Channel, error) {
  236. return s.Channel(channelID)
  237. }
  238. // Channel gets a channel by ID, it will look in all guilds an private channels.
  239. func (s *State) Channel(channelID string) (*Channel, error) {
  240. if s == nil {
  241. return nil, ErrNilState
  242. }
  243. if c, ok := s.channelMap[channelID]; ok {
  244. return c, nil
  245. }
  246. return nil, errors.New("Channel not found.")
  247. }
  248. // Emoji returns an emoji for a guild and emoji id.
  249. func (s *State) Emoji(guildID, emojiID string) (*Emoji, error) {
  250. if s == nil {
  251. return nil, ErrNilState
  252. }
  253. guild, err := s.Guild(guildID)
  254. if err != nil {
  255. return nil, err
  256. }
  257. s.RLock()
  258. defer s.RUnlock()
  259. for _, e := range guild.Emojis {
  260. if e.ID == emojiID {
  261. return e, nil
  262. }
  263. }
  264. return nil, errors.New("Emoji not found.")
  265. }
  266. // EmojiAdd adds an emoji to the current world state.
  267. func (s *State) EmojiAdd(guildID string, emoji *Emoji) error {
  268. if s == nil {
  269. return ErrNilState
  270. }
  271. guild, err := s.Guild(guildID)
  272. if err != nil {
  273. return err
  274. }
  275. s.Lock()
  276. defer s.Unlock()
  277. for i, e := range guild.Emojis {
  278. if e.ID == emoji.ID {
  279. guild.Emojis[i] = emoji
  280. return nil
  281. }
  282. }
  283. guild.Emojis = append(guild.Emojis, emoji)
  284. return nil
  285. }
  286. // EmojisAdd adds multiple emojis to the world state.
  287. func (s *State) EmojisAdd(guildID string, emojis []*Emoji) error {
  288. for _, e := range emojis {
  289. if err := s.EmojiAdd(guildID, e); err != nil {
  290. return err
  291. }
  292. }
  293. return nil
  294. }
  295. // MessageAdd adds a message to the current world state, or updates it if it exists.
  296. // If the channel cannot be found, the message is discarded.
  297. // Messages are kept in state up to s.MaxMessageCount
  298. func (s *State) MessageAdd(message *Message) error {
  299. if s == nil {
  300. return ErrNilState
  301. }
  302. if s.MaxMessageCount == 0 {
  303. return nil
  304. }
  305. c, err := s.Channel(message.ChannelID)
  306. if err != nil {
  307. return err
  308. }
  309. s.Lock()
  310. defer s.Unlock()
  311. // If the message exists, replace it.
  312. for i, m := range c.Messages {
  313. if m.ID == message.ID {
  314. c.Messages[i] = message
  315. return nil
  316. }
  317. }
  318. c.Messages = append(c.Messages, message)
  319. if len(c.Messages) > s.MaxMessageCount {
  320. c.Messages = c.Messages[len(c.Messages)-s.MaxMessageCount:]
  321. }
  322. return nil
  323. }
  324. // MessageRemove removes a message from the world state.
  325. func (s *State) MessageRemove(message *Message) error {
  326. if s == nil {
  327. return ErrNilState
  328. }
  329. if s.MaxMessageCount == 0 {
  330. return nil
  331. }
  332. c, err := s.Channel(message.ChannelID)
  333. if err != nil {
  334. return err
  335. }
  336. s.Lock()
  337. defer s.Unlock()
  338. for i, m := range c.Messages {
  339. if m.ID == message.ID {
  340. c.Messages = append(c.Messages[:i], c.Messages[i+1:]...)
  341. return nil
  342. }
  343. }
  344. return errors.New("Message not found.")
  345. }
  346. func (s *State) voiceStateUpdate(update *VoiceStateUpdate) error {
  347. guild, err := s.Guild(update.GuildID)
  348. if err != nil {
  349. return err
  350. }
  351. s.Lock()
  352. defer s.Unlock()
  353. // Handle Leaving Channel
  354. if update.ChannelID == "" {
  355. for i, state := range guild.VoiceStates {
  356. if state.UserID == update.UserID {
  357. guild.VoiceStates = append(guild.VoiceStates[:i], guild.VoiceStates[i+1:]...)
  358. return nil
  359. }
  360. }
  361. } else {
  362. for i, state := range guild.VoiceStates {
  363. if state.UserID == update.UserID {
  364. guild.VoiceStates[i] = update.VoiceState
  365. return nil
  366. }
  367. }
  368. guild.VoiceStates = append(guild.VoiceStates, update.VoiceState)
  369. }
  370. return nil
  371. }
  372. // Message gets a message by channel and message ID.
  373. func (s *State) Message(channelID, messageID string) (*Message, error) {
  374. if s == nil {
  375. return nil, ErrNilState
  376. }
  377. c, err := s.Channel(channelID)
  378. if err != nil {
  379. return nil, err
  380. }
  381. s.RLock()
  382. defer s.RUnlock()
  383. for _, m := range c.Messages {
  384. if m.ID == messageID {
  385. return m, nil
  386. }
  387. }
  388. return nil, errors.New("Message not found.")
  389. }
  390. // onInterface handles all events related to states.
  391. func (s *State) onInterface(se *Session, i interface{}) (err error) {
  392. if s == nil {
  393. return ErrNilState
  394. }
  395. if !se.StateEnabled {
  396. return nil
  397. }
  398. switch t := i.(type) {
  399. case *Ready:
  400. err = s.OnReady(t)
  401. case *GuildCreate:
  402. err = s.GuildAdd(t.Guild)
  403. case *GuildUpdate:
  404. err = s.GuildAdd(t.Guild)
  405. case *GuildDelete:
  406. err = s.GuildRemove(t.Guild)
  407. case *GuildMemberAdd:
  408. err = s.MemberAdd(t.Member)
  409. case *GuildMemberUpdate:
  410. err = s.MemberAdd(t.Member)
  411. case *GuildMemberRemove:
  412. err = s.MemberRemove(t.Member)
  413. case *GuildEmojisUpdate:
  414. err = s.EmojisAdd(t.GuildID, t.Emojis)
  415. case *ChannelCreate:
  416. err = s.ChannelAdd(t.Channel)
  417. case *ChannelUpdate:
  418. err = s.ChannelAdd(t.Channel)
  419. case *ChannelDelete:
  420. err = s.ChannelRemove(t.Channel)
  421. case *MessageCreate:
  422. err = s.MessageAdd(t.Message)
  423. case *MessageUpdate:
  424. err = s.MessageAdd(t.Message)
  425. case *MessageDelete:
  426. err = s.MessageRemove(t.Message)
  427. case *VoiceStateUpdate:
  428. err = s.voiceStateUpdate(t)
  429. }
  430. return
  431. }