state.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. }
  22. }
  23. // OnReady takes a Ready event and updates all internal state.
  24. func (s *State) OnReady(r *Ready) error {
  25. if s == nil {
  26. return ErrNilState
  27. }
  28. s.Lock()
  29. defer s.Unlock()
  30. s.Ready = *r
  31. for _, g := range s.Guilds {
  32. for _, c := range g.Channels {
  33. c.GuildID = g.ID
  34. }
  35. }
  36. return nil
  37. }
  38. // GuildAdd adds a guild to the current world state, or
  39. // updates it if it already exists.
  40. func (s *State) GuildAdd(guild *Guild) error {
  41. if s == nil {
  42. return ErrNilState
  43. }
  44. s.Lock()
  45. defer s.Unlock()
  46. // If the guild exists, replace it.
  47. for i, g := range s.Guilds {
  48. if g.ID == guild.ID {
  49. // Don't stomp on properties that don't come in updates.
  50. guild.Members = g.Members
  51. guild.Presences = g.Presences
  52. guild.Channels = g.Channels
  53. guild.VoiceStates = g.VoiceStates
  54. s.Guilds[i] = guild
  55. return nil
  56. }
  57. }
  58. // Otherwise, update the channels to point to the right guild
  59. for _, c := range guild.Channels {
  60. c.GuildID = guild.ID
  61. }
  62. s.Guilds = append(s.Guilds, guild)
  63. return nil
  64. }
  65. // GuildRemove removes a guild from current world state.
  66. func (s *State) GuildRemove(guild *Guild) error {
  67. if s == nil {
  68. return ErrNilState
  69. }
  70. s.Lock()
  71. defer s.Unlock()
  72. for i, g := range s.Guilds {
  73. if g.ID == guild.ID {
  74. s.Guilds = append(s.Guilds[:i], s.Guilds[i+1:]...)
  75. return nil
  76. }
  77. }
  78. return errors.New("Guild not found.")
  79. }
  80. // Guild gets a guild by ID.
  81. // Useful for querying if @me is in a guild:
  82. // _, err := discordgo.Session.State.Guild(guildID)
  83. // isInGuild := err == nil
  84. func (s *State) Guild(guildID string) (*Guild, error) {
  85. if s == nil {
  86. return nil, ErrNilState
  87. }
  88. s.RLock()
  89. defer s.RUnlock()
  90. for _, g := range s.Guilds {
  91. if g.ID == guildID {
  92. return g, nil
  93. }
  94. }
  95. return nil, errors.New("Guild not found.")
  96. }
  97. // TODO: Consider moving Guild state update methods onto *Guild.
  98. // MemberAdd adds a member to the current world state, or
  99. // updates it if it already exists.
  100. func (s *State) MemberAdd(member *Member) error {
  101. if s == nil {
  102. return ErrNilState
  103. }
  104. guild, err := s.Guild(member.GuildID)
  105. if err != nil {
  106. return err
  107. }
  108. s.Lock()
  109. defer s.Unlock()
  110. for i, m := range guild.Members {
  111. if m.User.ID == member.User.ID {
  112. guild.Members[i] = member
  113. return nil
  114. }
  115. }
  116. guild.Members = append(guild.Members, member)
  117. return nil
  118. }
  119. // MemberRemove removes a member from current world state.
  120. func (s *State) MemberRemove(member *Member) error {
  121. if s == nil {
  122. return ErrNilState
  123. }
  124. guild, err := s.Guild(member.GuildID)
  125. if err != nil {
  126. return err
  127. }
  128. s.Lock()
  129. defer s.Unlock()
  130. for i, m := range guild.Members {
  131. if m.User.ID == member.User.ID {
  132. guild.Members = append(guild.Members[:i], guild.Members[i+1:]...)
  133. return nil
  134. }
  135. }
  136. return errors.New("Member not found.")
  137. }
  138. // Member gets a member by ID from a guild.
  139. func (s *State) Member(guildID, userID string) (*Member, error) {
  140. if s == nil {
  141. return nil, ErrNilState
  142. }
  143. guild, err := s.Guild(guildID)
  144. if err != nil {
  145. return nil, err
  146. }
  147. s.RLock()
  148. defer s.RUnlock()
  149. for _, m := range guild.Members {
  150. if m.User.ID == userID {
  151. return m, nil
  152. }
  153. }
  154. return nil, errors.New("Member not found.")
  155. }
  156. // ChannelAdd adds a guild to the current world state, or
  157. // updates it if it already exists.
  158. // Channels may exist either as PrivateChannels or inside
  159. // a guild.
  160. func (s *State) ChannelAdd(channel *Channel) error {
  161. if s == nil {
  162. return ErrNilState
  163. }
  164. if channel.IsPrivate {
  165. s.Lock()
  166. defer s.Unlock()
  167. // If the channel exists, replace it.
  168. for i, c := range s.PrivateChannels {
  169. if c.ID == channel.ID {
  170. // Don't stomp on messages.
  171. channel.Messages = c.Messages
  172. s.PrivateChannels[i] = channel
  173. return nil
  174. }
  175. }
  176. s.PrivateChannels = append(s.PrivateChannels, channel)
  177. } else {
  178. guild, err := s.Guild(channel.GuildID)
  179. if err != nil {
  180. return err
  181. }
  182. s.Lock()
  183. defer s.Unlock()
  184. // If the channel exists, replace it.
  185. for i, c := range guild.Channels {
  186. if c.ID == channel.ID {
  187. // Don't stomp on messages.
  188. channel.Messages = c.Messages
  189. guild.Channels[i] = channel
  190. return nil
  191. }
  192. }
  193. guild.Channels = append(guild.Channels, channel)
  194. }
  195. return nil
  196. }
  197. // ChannelRemove removes a channel from current world state.
  198. func (s *State) ChannelRemove(channel *Channel) error {
  199. if s == nil {
  200. return ErrNilState
  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. return errors.New("Channel not found.")
  226. }
  227. // GuildChannel gets a channel by ID from a guild.
  228. func (s *State) GuildChannel(guildID, channelID string) (*Channel, error) {
  229. if s == nil {
  230. return nil, ErrNilState
  231. }
  232. guild, err := s.Guild(guildID)
  233. if err != nil {
  234. return nil, err
  235. }
  236. s.RLock()
  237. defer s.RUnlock()
  238. for _, c := range guild.Channels {
  239. if c.ID == channelID {
  240. return c, nil
  241. }
  242. }
  243. return nil, errors.New("Channel not found.")
  244. }
  245. // PrivateChannel gets a private channel by ID.
  246. func (s *State) PrivateChannel(channelID string) (*Channel, error) {
  247. if s == nil {
  248. return nil, ErrNilState
  249. }
  250. s.RLock()
  251. defer s.RUnlock()
  252. for _, c := range s.PrivateChannels {
  253. if c.ID == channelID {
  254. return c, nil
  255. }
  256. }
  257. return nil, errors.New("Channel not found.")
  258. }
  259. // Channel gets a channel by ID, it will look in all guilds an private channels.
  260. func (s *State) Channel(channelID string) (*Channel, error) {
  261. if s == nil {
  262. return nil, ErrNilState
  263. }
  264. c, err := s.PrivateChannel(channelID)
  265. if err == nil {
  266. return c, nil
  267. }
  268. for _, g := range s.Guilds {
  269. c, err := s.GuildChannel(g.ID, channelID)
  270. if err == nil {
  271. return c, nil
  272. }
  273. }
  274. return nil, errors.New("Channel not found.")
  275. }
  276. // Emoji returns an emoji for a guild and emoji id.
  277. func (s *State) Emoji(guildID, emojiID string) (*Emoji, error) {
  278. if s == nil {
  279. return nil, ErrNilState
  280. }
  281. guild, err := s.Guild(guildID)
  282. if err != nil {
  283. return nil, err
  284. }
  285. s.RLock()
  286. defer s.RUnlock()
  287. for _, e := range guild.Emojis {
  288. if e.ID == emojiID {
  289. return e, nil
  290. }
  291. }
  292. return nil, errors.New("Emoji not found.")
  293. }
  294. // EmojiAdd adds an emoji to the current world state.
  295. func (s *State) EmojiAdd(guildID string, emoji *Emoji) error {
  296. if s == nil {
  297. return ErrNilState
  298. }
  299. guild, err := s.Guild(guildID)
  300. if err != nil {
  301. return err
  302. }
  303. s.Lock()
  304. defer s.Unlock()
  305. for i, e := range guild.Emojis {
  306. if e.ID == emoji.ID {
  307. guild.Emojis[i] = emoji
  308. return nil
  309. }
  310. }
  311. guild.Emojis = append(guild.Emojis, emoji)
  312. return nil
  313. }
  314. // EmojisAdd adds multiple emojis to the world state.
  315. func (s *State) EmojisAdd(guildID string, emojis []*Emoji) error {
  316. for _, e := range emojis {
  317. if err := s.EmojiAdd(guildID, e); err != nil {
  318. return err
  319. }
  320. }
  321. return nil
  322. }
  323. // MessageAdd adds a message to the current world state, or updates it if it exists.
  324. // If the channel cannot be found, the message is discarded.
  325. // Messages are kept in state up to s.MaxMessageCount
  326. func (s *State) MessageAdd(message *Message) error {
  327. if s == nil {
  328. return ErrNilState
  329. }
  330. c, err := s.Channel(message.ChannelID)
  331. if err != nil {
  332. return err
  333. }
  334. s.Lock()
  335. defer s.Unlock()
  336. // If the message exists, replace it.
  337. for i, m := range c.Messages {
  338. if m.ID == message.ID {
  339. c.Messages[i] = message
  340. return nil
  341. }
  342. }
  343. c.Messages = append(c.Messages, message)
  344. if len(c.Messages) > s.MaxMessageCount {
  345. c.Messages = c.Messages[len(c.Messages)-s.MaxMessageCount:]
  346. }
  347. return nil
  348. }
  349. // MessageRemove removes a message from the world state.
  350. func (s *State) MessageRemove(message *Message) error {
  351. if s == nil {
  352. return ErrNilState
  353. }
  354. c, err := s.Channel(message.ChannelID)
  355. if err != nil {
  356. return err
  357. }
  358. s.Lock()
  359. defer s.Unlock()
  360. for i, m := range c.Messages {
  361. if m.ID == message.ID {
  362. c.Messages = append(c.Messages[:i], c.Messages[i+1:]...)
  363. return nil
  364. }
  365. }
  366. return errors.New("Message not found.")
  367. }
  368. func (s *State) voiceStateUpdate(update *VoiceStateUpdate) error {
  369. guild, err := s.Guild(update.GuildID)
  370. if err != nil {
  371. return err
  372. }
  373. s.Lock()
  374. defer s.Unlock()
  375. // Handle Leaving Channel
  376. if update.ChannelID == "" {
  377. for i, state := range guild.VoiceStates {
  378. if state.UserID == update.UserID {
  379. guild.VoiceStates = append(guild.VoiceStates[:i], guild.VoiceStates[i+1:]...)
  380. return nil
  381. }
  382. }
  383. } else {
  384. for i, state := range guild.VoiceStates {
  385. if state.UserID == update.UserID {
  386. guild.VoiceStates[i] = update.VoiceState
  387. return nil
  388. }
  389. }
  390. guild.VoiceStates = append(guild.VoiceStates, update.VoiceState)
  391. }
  392. return nil
  393. }
  394. // Message gets a message by channel and message ID.
  395. func (s *State) Message(channelID, messageID string) (*Message, error) {
  396. if s == nil {
  397. return nil, ErrNilState
  398. }
  399. c, err := s.Channel(channelID)
  400. if err != nil {
  401. return nil, err
  402. }
  403. s.RLock()
  404. defer s.RUnlock()
  405. for _, m := range c.Messages {
  406. if m.ID == messageID {
  407. return m, nil
  408. }
  409. }
  410. return nil, errors.New("Message not found.")
  411. }
  412. // onInterface handles all events related to states.
  413. func (s *State) onInterface(se *Session, i interface{}) (err error) {
  414. if s == nil {
  415. return ErrNilState
  416. }
  417. if !se.StateEnabled {
  418. return nil
  419. }
  420. switch t := i.(type) {
  421. case *Ready:
  422. err = s.OnReady(t)
  423. case *GuildCreate:
  424. err = s.GuildAdd(t.Guild)
  425. case *GuildUpdate:
  426. err = s.GuildAdd(t.Guild)
  427. case *GuildDelete:
  428. err = s.GuildRemove(t.Guild)
  429. case *GuildMemberAdd:
  430. err = s.MemberAdd(t.Member)
  431. case *GuildMemberUpdate:
  432. err = s.MemberAdd(t.Member)
  433. case *GuildMemberRemove:
  434. err = s.MemberRemove(t.Member)
  435. case *GuildEmojisUpdate:
  436. err = s.EmojisAdd(t.GuildID, t.Emojis)
  437. case *ChannelCreate:
  438. err = s.ChannelAdd(t.Channel)
  439. case *ChannelUpdate:
  440. err = s.ChannelAdd(t.Channel)
  441. case *ChannelDelete:
  442. err = s.ChannelRemove(t.Channel)
  443. case *MessageCreate:
  444. err = s.MessageAdd(t.Message)
  445. case *MessageUpdate:
  446. err = s.MessageAdd(t.Message)
  447. case *MessageDelete:
  448. err = s.MessageRemove(t.Message)
  449. case *VoiceStateUpdate:
  450. err = s.voiceStateUpdate(t)
  451. }
  452. return
  453. }