state.go 11 KB

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