state.go 11 KB

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