state.go 11 KB

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