state.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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 (
  12. "errors"
  13. "sort"
  14. "sync"
  15. )
  16. // ErrNilState is returned when the state is nil.
  17. var ErrNilState = errors.New("state not instantiated, please use discordgo.New() or assign Session.State")
  18. // ErrStateNotFound is returned when the state cache
  19. // requested is not found
  20. var ErrStateNotFound = errors.New("state cache not found")
  21. // A State contains the current known state.
  22. // As discord sends this in a READY blob, it seems reasonable to simply
  23. // use that struct as the data store.
  24. type State struct {
  25. sync.RWMutex
  26. Ready
  27. MaxMessageCount int
  28. TrackChannels bool
  29. TrackEmojis bool
  30. TrackMembers bool
  31. TrackRoles bool
  32. TrackVoice bool
  33. TrackPresences bool
  34. guildMap map[string]*Guild
  35. channelMap map[string]*Channel
  36. }
  37. // NewState creates an empty state.
  38. func NewState() *State {
  39. return &State{
  40. Ready: Ready{
  41. PrivateChannels: []*Channel{},
  42. Guilds: []*Guild{},
  43. },
  44. TrackChannels: true,
  45. TrackEmojis: true,
  46. TrackMembers: true,
  47. TrackRoles: true,
  48. TrackVoice: true,
  49. TrackPresences: true,
  50. guildMap: make(map[string]*Guild),
  51. channelMap: make(map[string]*Channel),
  52. }
  53. }
  54. // GuildAdd adds a guild to the current world state, or
  55. // updates it if it already exists.
  56. func (s *State) GuildAdd(guild *Guild) error {
  57. if s == nil {
  58. return ErrNilState
  59. }
  60. s.Lock()
  61. defer s.Unlock()
  62. // Update the channels to point to the right guild, adding them to the channelMap as we go
  63. for _, c := range guild.Channels {
  64. s.channelMap[c.ID] = c
  65. }
  66. if g, ok := s.guildMap[guild.ID]; ok {
  67. // We are about to replace `g` in the state with `guild`, but first we need to
  68. // make sure we preserve any fields that the `guild` doesn't contain from `g`.
  69. if guild.Roles == nil {
  70. guild.Roles = g.Roles
  71. }
  72. if guild.Emojis == nil {
  73. guild.Emojis = g.Emojis
  74. }
  75. if guild.Members == nil {
  76. guild.Members = g.Members
  77. }
  78. if guild.Presences == nil {
  79. guild.Presences = g.Presences
  80. }
  81. if guild.Channels == nil {
  82. guild.Channels = g.Channels
  83. }
  84. if guild.VoiceStates == nil {
  85. guild.VoiceStates = g.VoiceStates
  86. }
  87. *g = *guild
  88. return nil
  89. }
  90. s.Guilds = append(s.Guilds, guild)
  91. s.guildMap[guild.ID] = guild
  92. return nil
  93. }
  94. // GuildRemove removes a guild from current world state.
  95. func (s *State) GuildRemove(guild *Guild) error {
  96. if s == nil {
  97. return ErrNilState
  98. }
  99. _, err := s.Guild(guild.ID)
  100. if err != nil {
  101. return err
  102. }
  103. s.Lock()
  104. defer s.Unlock()
  105. delete(s.guildMap, guild.ID)
  106. for i, g := range s.Guilds {
  107. if g.ID == guild.ID {
  108. s.Guilds = append(s.Guilds[:i], s.Guilds[i+1:]...)
  109. return nil
  110. }
  111. }
  112. return nil
  113. }
  114. // Guild gets a guild by ID.
  115. // Useful for querying if @me is in a guild:
  116. // _, err := discordgo.Session.State.Guild(guildID)
  117. // isInGuild := err == nil
  118. func (s *State) Guild(guildID string) (*Guild, error) {
  119. if s == nil {
  120. return nil, ErrNilState
  121. }
  122. s.RLock()
  123. defer s.RUnlock()
  124. if g, ok := s.guildMap[guildID]; ok {
  125. return g, nil
  126. }
  127. return nil, ErrStateNotFound
  128. }
  129. // PresenceAdd adds a presence to the current world state, or
  130. // updates it if it already exists.
  131. func (s *State) PresenceAdd(guildID string, presence *Presence) error {
  132. if s == nil {
  133. return ErrNilState
  134. }
  135. guild, err := s.Guild(guildID)
  136. if err != nil {
  137. return err
  138. }
  139. s.Lock()
  140. defer s.Unlock()
  141. for i, p := range guild.Presences {
  142. if p.User.ID == presence.User.ID {
  143. //guild.Presences[i] = presence
  144. //Update status
  145. guild.Presences[i].Game = presence.Game
  146. guild.Presences[i].Roles = presence.Roles
  147. if presence.Status != "" {
  148. guild.Presences[i].Status = presence.Status
  149. }
  150. if presence.Nick != "" {
  151. guild.Presences[i].Nick = presence.Nick
  152. }
  153. //Update the optionally sent user information
  154. //ID Is a mandatory field so you should not need to check if it is empty
  155. guild.Presences[i].User.ID = presence.User.ID
  156. if presence.User.Avatar != "" {
  157. guild.Presences[i].User.Avatar = presence.User.Avatar
  158. }
  159. if presence.User.Discriminator != "" {
  160. guild.Presences[i].User.Discriminator = presence.User.Discriminator
  161. }
  162. if presence.User.Email != "" {
  163. guild.Presences[i].User.Email = presence.User.Email
  164. }
  165. if presence.User.Token != "" {
  166. guild.Presences[i].User.Token = presence.User.Token
  167. }
  168. if presence.User.Username != "" {
  169. guild.Presences[i].User.Username = presence.User.Username
  170. }
  171. return nil
  172. }
  173. }
  174. guild.Presences = append(guild.Presences, presence)
  175. return nil
  176. }
  177. // PresenceRemove removes a presence from the current world state.
  178. func (s *State) PresenceRemove(guildID string, presence *Presence) error {
  179. if s == nil {
  180. return ErrNilState
  181. }
  182. guild, err := s.Guild(guildID)
  183. if err != nil {
  184. return err
  185. }
  186. s.Lock()
  187. defer s.Unlock()
  188. for i, p := range guild.Presences {
  189. if p.User.ID == presence.User.ID {
  190. guild.Presences = append(guild.Presences[:i], guild.Presences[i+1:]...)
  191. return nil
  192. }
  193. }
  194. return ErrStateNotFound
  195. }
  196. // Presence gets a presence by ID from a guild.
  197. func (s *State) Presence(guildID, userID string) (*Presence, error) {
  198. if s == nil {
  199. return nil, ErrNilState
  200. }
  201. guild, err := s.Guild(guildID)
  202. if err != nil {
  203. return nil, err
  204. }
  205. for _, p := range guild.Presences {
  206. if p.User.ID == userID {
  207. return p, nil
  208. }
  209. }
  210. return nil, ErrStateNotFound
  211. }
  212. // TODO: Consider moving Guild state update methods onto *Guild.
  213. // MemberAdd adds a member to the current world state, or
  214. // updates it if it already exists.
  215. func (s *State) MemberAdd(member *Member) error {
  216. if s == nil {
  217. return ErrNilState
  218. }
  219. guild, err := s.Guild(member.GuildID)
  220. if err != nil {
  221. return err
  222. }
  223. s.Lock()
  224. defer s.Unlock()
  225. for i, m := range guild.Members {
  226. if m.User.ID == member.User.ID {
  227. guild.Members[i] = member
  228. return nil
  229. }
  230. }
  231. guild.Members = append(guild.Members, member)
  232. return nil
  233. }
  234. // MemberRemove removes a member from current world state.
  235. func (s *State) MemberRemove(member *Member) error {
  236. if s == nil {
  237. return ErrNilState
  238. }
  239. guild, err := s.Guild(member.GuildID)
  240. if err != nil {
  241. return err
  242. }
  243. s.Lock()
  244. defer s.Unlock()
  245. for i, m := range guild.Members {
  246. if m.User.ID == member.User.ID {
  247. guild.Members = append(guild.Members[:i], guild.Members[i+1:]...)
  248. return nil
  249. }
  250. }
  251. return ErrStateNotFound
  252. }
  253. // Member gets a member by ID from a guild.
  254. func (s *State) Member(guildID, userID string) (*Member, error) {
  255. if s == nil {
  256. return nil, ErrNilState
  257. }
  258. guild, err := s.Guild(guildID)
  259. if err != nil {
  260. return nil, err
  261. }
  262. s.RLock()
  263. defer s.RUnlock()
  264. for _, m := range guild.Members {
  265. if m.User.ID == userID {
  266. return m, nil
  267. }
  268. }
  269. return nil, ErrStateNotFound
  270. }
  271. // RoleAdd adds a role to the current world state, or
  272. // updates it if it already exists.
  273. func (s *State) RoleAdd(guildID string, role *Role) error {
  274. if s == nil {
  275. return ErrNilState
  276. }
  277. guild, err := s.Guild(guildID)
  278. if err != nil {
  279. return err
  280. }
  281. s.Lock()
  282. defer s.Unlock()
  283. for i, r := range guild.Roles {
  284. if r.ID == role.ID {
  285. guild.Roles[i] = role
  286. return nil
  287. }
  288. }
  289. guild.Roles = append(guild.Roles, role)
  290. return nil
  291. }
  292. // RoleRemove removes a role from current world state by ID.
  293. func (s *State) RoleRemove(guildID, roleID string) error {
  294. if s == nil {
  295. return ErrNilState
  296. }
  297. guild, err := s.Guild(guildID)
  298. if err != nil {
  299. return err
  300. }
  301. s.Lock()
  302. defer s.Unlock()
  303. for i, r := range guild.Roles {
  304. if r.ID == roleID {
  305. guild.Roles = append(guild.Roles[:i], guild.Roles[i+1:]...)
  306. return nil
  307. }
  308. }
  309. return ErrStateNotFound
  310. }
  311. // Role gets a role by ID from a guild.
  312. func (s *State) Role(guildID, roleID string) (*Role, error) {
  313. if s == nil {
  314. return nil, ErrNilState
  315. }
  316. guild, err := s.Guild(guildID)
  317. if err != nil {
  318. return nil, err
  319. }
  320. s.RLock()
  321. defer s.RUnlock()
  322. for _, r := range guild.Roles {
  323. if r.ID == roleID {
  324. return r, nil
  325. }
  326. }
  327. return nil, ErrStateNotFound
  328. }
  329. // ChannelAdd adds a channel to the current world state, or
  330. // updates it if it already exists.
  331. // Channels may exist either as PrivateChannels or inside
  332. // a guild.
  333. func (s *State) ChannelAdd(channel *Channel) error {
  334. if s == nil {
  335. return ErrNilState
  336. }
  337. s.Lock()
  338. defer s.Unlock()
  339. // If the channel exists, replace it
  340. if c, ok := s.channelMap[channel.ID]; ok {
  341. if channel.Messages == nil {
  342. channel.Messages = c.Messages
  343. }
  344. if channel.PermissionOverwrites == nil {
  345. channel.PermissionOverwrites = c.PermissionOverwrites
  346. }
  347. *c = *channel
  348. return nil
  349. }
  350. if channel.Type == ChannelTypeDM || channel.Type == ChannelTypeGroupDM {
  351. s.PrivateChannels = append(s.PrivateChannels, channel)
  352. } else {
  353. guild, ok := s.guildMap[channel.GuildID]
  354. if !ok {
  355. return ErrStateNotFound
  356. }
  357. guild.Channels = append(guild.Channels, channel)
  358. }
  359. s.channelMap[channel.ID] = channel
  360. return nil
  361. }
  362. // ChannelRemove removes a channel from current world state.
  363. func (s *State) ChannelRemove(channel *Channel) error {
  364. if s == nil {
  365. return ErrNilState
  366. }
  367. _, err := s.Channel(channel.ID)
  368. if err != nil {
  369. return err
  370. }
  371. if channel.Type == ChannelTypeDM || channel.Type == ChannelTypeGroupDM {
  372. s.Lock()
  373. defer s.Unlock()
  374. for i, c := range s.PrivateChannels {
  375. if c.ID == channel.ID {
  376. s.PrivateChannels = append(s.PrivateChannels[:i], s.PrivateChannels[i+1:]...)
  377. break
  378. }
  379. }
  380. } else {
  381. guild, err := s.Guild(channel.GuildID)
  382. if err != nil {
  383. return err
  384. }
  385. s.Lock()
  386. defer s.Unlock()
  387. for i, c := range guild.Channels {
  388. if c.ID == channel.ID {
  389. guild.Channels = append(guild.Channels[:i], guild.Channels[i+1:]...)
  390. break
  391. }
  392. }
  393. }
  394. delete(s.channelMap, channel.ID)
  395. return nil
  396. }
  397. // GuildChannel gets a channel by ID from a guild.
  398. // This method is Deprecated, use Channel(channelID)
  399. func (s *State) GuildChannel(guildID, channelID string) (*Channel, error) {
  400. return s.Channel(channelID)
  401. }
  402. // PrivateChannel gets a private channel by ID.
  403. // This method is Deprecated, use Channel(channelID)
  404. func (s *State) PrivateChannel(channelID string) (*Channel, error) {
  405. return s.Channel(channelID)
  406. }
  407. // Channel gets a channel by ID, it will look in all guilds an private channels.
  408. func (s *State) Channel(channelID string) (*Channel, error) {
  409. if s == nil {
  410. return nil, ErrNilState
  411. }
  412. s.RLock()
  413. defer s.RUnlock()
  414. if c, ok := s.channelMap[channelID]; ok {
  415. return c, nil
  416. }
  417. return nil, ErrStateNotFound
  418. }
  419. // Emoji returns an emoji for a guild and emoji id.
  420. func (s *State) Emoji(guildID, emojiID string) (*Emoji, error) {
  421. if s == nil {
  422. return nil, ErrNilState
  423. }
  424. guild, err := s.Guild(guildID)
  425. if err != nil {
  426. return nil, err
  427. }
  428. s.RLock()
  429. defer s.RUnlock()
  430. for _, e := range guild.Emojis {
  431. if e.ID == emojiID {
  432. return e, nil
  433. }
  434. }
  435. return nil, ErrStateNotFound
  436. }
  437. // EmojiAdd adds an emoji to the current world state.
  438. func (s *State) EmojiAdd(guildID string, emoji *Emoji) error {
  439. if s == nil {
  440. return ErrNilState
  441. }
  442. guild, err := s.Guild(guildID)
  443. if err != nil {
  444. return err
  445. }
  446. s.Lock()
  447. defer s.Unlock()
  448. for i, e := range guild.Emojis {
  449. if e.ID == emoji.ID {
  450. guild.Emojis[i] = emoji
  451. return nil
  452. }
  453. }
  454. guild.Emojis = append(guild.Emojis, emoji)
  455. return nil
  456. }
  457. // EmojisAdd adds multiple emojis to the world state.
  458. func (s *State) EmojisAdd(guildID string, emojis []*Emoji) error {
  459. for _, e := range emojis {
  460. if err := s.EmojiAdd(guildID, e); err != nil {
  461. return err
  462. }
  463. }
  464. return nil
  465. }
  466. // MessageAdd adds a message to the current world state, or updates it if it exists.
  467. // If the channel cannot be found, the message is discarded.
  468. // Messages are kept in state up to s.MaxMessageCount
  469. func (s *State) MessageAdd(message *Message) error {
  470. if s == nil {
  471. return ErrNilState
  472. }
  473. c, err := s.Channel(message.ChannelID)
  474. if err != nil {
  475. return err
  476. }
  477. s.Lock()
  478. defer s.Unlock()
  479. // If the message exists, merge in the new message contents.
  480. for _, m := range c.Messages {
  481. if m.ID == message.ID {
  482. if message.Content != "" {
  483. m.Content = message.Content
  484. }
  485. if message.EditedTimestamp != "" {
  486. m.EditedTimestamp = message.EditedTimestamp
  487. }
  488. if message.Mentions != nil {
  489. m.Mentions = message.Mentions
  490. }
  491. if message.Embeds != nil {
  492. m.Embeds = message.Embeds
  493. }
  494. if message.Attachments != nil {
  495. m.Attachments = message.Attachments
  496. }
  497. if message.Timestamp != "" {
  498. m.Timestamp = message.Timestamp
  499. }
  500. if message.Author != nil {
  501. m.Author = message.Author
  502. }
  503. return nil
  504. }
  505. }
  506. c.Messages = append(c.Messages, message)
  507. if len(c.Messages) > s.MaxMessageCount {
  508. c.Messages = c.Messages[len(c.Messages)-s.MaxMessageCount:]
  509. }
  510. return nil
  511. }
  512. // MessageRemove removes a message from the world state.
  513. func (s *State) MessageRemove(message *Message) error {
  514. if s == nil {
  515. return ErrNilState
  516. }
  517. return s.messageRemoveByID(message.ChannelID, message.ID)
  518. }
  519. // messageRemoveByID removes a message by channelID and messageID from the world state.
  520. func (s *State) messageRemoveByID(channelID, messageID string) error {
  521. c, err := s.Channel(channelID)
  522. if err != nil {
  523. return err
  524. }
  525. s.Lock()
  526. defer s.Unlock()
  527. for i, m := range c.Messages {
  528. if m.ID == messageID {
  529. c.Messages = append(c.Messages[:i], c.Messages[i+1:]...)
  530. return nil
  531. }
  532. }
  533. return ErrStateNotFound
  534. }
  535. func (s *State) voiceStateUpdate(update *VoiceStateUpdate) error {
  536. guild, err := s.Guild(update.GuildID)
  537. if err != nil {
  538. return err
  539. }
  540. s.Lock()
  541. defer s.Unlock()
  542. // Handle Leaving Channel
  543. if update.ChannelID == "" {
  544. for i, state := range guild.VoiceStates {
  545. if state.UserID == update.UserID {
  546. guild.VoiceStates = append(guild.VoiceStates[:i], guild.VoiceStates[i+1:]...)
  547. return nil
  548. }
  549. }
  550. } else {
  551. for i, state := range guild.VoiceStates {
  552. if state.UserID == update.UserID {
  553. guild.VoiceStates[i] = update.VoiceState
  554. return nil
  555. }
  556. }
  557. guild.VoiceStates = append(guild.VoiceStates, update.VoiceState)
  558. }
  559. return nil
  560. }
  561. // Message gets a message by channel and message ID.
  562. func (s *State) Message(channelID, messageID string) (*Message, error) {
  563. if s == nil {
  564. return nil, ErrNilState
  565. }
  566. c, err := s.Channel(channelID)
  567. if err != nil {
  568. return nil, err
  569. }
  570. s.RLock()
  571. defer s.RUnlock()
  572. for _, m := range c.Messages {
  573. if m.ID == messageID {
  574. return m, nil
  575. }
  576. }
  577. return nil, ErrStateNotFound
  578. }
  579. // OnReady takes a Ready event and updates all internal state.
  580. func (s *State) onReady(se *Session, r *Ready) (err error) {
  581. if s == nil {
  582. return ErrNilState
  583. }
  584. s.Lock()
  585. defer s.Unlock()
  586. // We must track at least the current user for Voice, even
  587. // if state is disabled, store the bare essentials.
  588. if !se.StateEnabled {
  589. ready := Ready{
  590. Version: r.Version,
  591. SessionID: r.SessionID,
  592. User: r.User,
  593. }
  594. s.Ready = ready
  595. return nil
  596. }
  597. s.Ready = *r
  598. for _, g := range s.Guilds {
  599. s.guildMap[g.ID] = g
  600. for _, c := range g.Channels {
  601. s.channelMap[c.ID] = c
  602. }
  603. }
  604. for _, c := range s.PrivateChannels {
  605. s.channelMap[c.ID] = c
  606. }
  607. return nil
  608. }
  609. // onInterface handles all events related to states.
  610. func (s *State) onInterface(se *Session, i interface{}) (err error) {
  611. if s == nil {
  612. return ErrNilState
  613. }
  614. r, ok := i.(*Ready)
  615. if ok {
  616. return s.onReady(se, r)
  617. }
  618. if !se.StateEnabled {
  619. return nil
  620. }
  621. switch t := i.(type) {
  622. case *GuildCreate:
  623. err = s.GuildAdd(t.Guild)
  624. case *GuildUpdate:
  625. err = s.GuildAdd(t.Guild)
  626. case *GuildDelete:
  627. err = s.GuildRemove(t.Guild)
  628. case *GuildMemberAdd:
  629. if s.TrackMembers {
  630. err = s.MemberAdd(t.Member)
  631. }
  632. case *GuildMemberUpdate:
  633. if s.TrackMembers {
  634. err = s.MemberAdd(t.Member)
  635. }
  636. case *GuildMemberRemove:
  637. if s.TrackMembers {
  638. err = s.MemberRemove(t.Member)
  639. }
  640. case *GuildRoleCreate:
  641. if s.TrackRoles {
  642. err = s.RoleAdd(t.GuildID, t.Role)
  643. }
  644. case *GuildRoleUpdate:
  645. if s.TrackRoles {
  646. err = s.RoleAdd(t.GuildID, t.Role)
  647. }
  648. case *GuildRoleDelete:
  649. if s.TrackRoles {
  650. err = s.RoleRemove(t.GuildID, t.RoleID)
  651. }
  652. case *GuildEmojisUpdate:
  653. if s.TrackEmojis {
  654. err = s.EmojisAdd(t.GuildID, t.Emojis)
  655. }
  656. case *ChannelCreate:
  657. if s.TrackChannels {
  658. err = s.ChannelAdd(t.Channel)
  659. }
  660. case *ChannelUpdate:
  661. if s.TrackChannels {
  662. err = s.ChannelAdd(t.Channel)
  663. }
  664. case *ChannelDelete:
  665. if s.TrackChannels {
  666. err = s.ChannelRemove(t.Channel)
  667. }
  668. case *MessageCreate:
  669. if s.MaxMessageCount != 0 {
  670. err = s.MessageAdd(t.Message)
  671. }
  672. case *MessageUpdate:
  673. if s.MaxMessageCount != 0 {
  674. err = s.MessageAdd(t.Message)
  675. }
  676. case *MessageDelete:
  677. if s.MaxMessageCount != 0 {
  678. err = s.MessageRemove(t.Message)
  679. }
  680. case *MessageDeleteBulk:
  681. if s.MaxMessageCount != 0 {
  682. for _, mID := range t.Messages {
  683. s.messageRemoveByID(t.ChannelID, mID)
  684. }
  685. }
  686. case *VoiceStateUpdate:
  687. if s.TrackVoice {
  688. err = s.voiceStateUpdate(t)
  689. }
  690. case *PresenceUpdate:
  691. if s.TrackPresences {
  692. s.PresenceAdd(t.GuildID, &t.Presence)
  693. }
  694. if s.TrackMembers {
  695. if t.Status == StatusOffline {
  696. return
  697. }
  698. var m *Member
  699. m, err = s.Member(t.GuildID, t.User.ID)
  700. if err != nil {
  701. // Member not found; this is a user coming online
  702. m = &Member{
  703. GuildID: t.GuildID,
  704. Nick: t.Nick,
  705. User: t.User,
  706. Roles: t.Roles,
  707. }
  708. } else {
  709. if t.Nick != "" {
  710. m.Nick = t.Nick
  711. }
  712. if t.User.Username != "" {
  713. m.User.Username = t.User.Username
  714. }
  715. // PresenceUpdates always contain a list of roles, so there's no need to check for an empty list here
  716. m.Roles = t.Roles
  717. }
  718. err = s.MemberAdd(m)
  719. }
  720. }
  721. return
  722. }
  723. // UserChannelPermissions returns the permission of a user in a channel.
  724. // userID : The ID of the user to calculate permissions for.
  725. // channelID : The ID of the channel to calculate permission for.
  726. func (s *State) UserChannelPermissions(userID, channelID string) (apermissions int, err error) {
  727. if s == nil {
  728. return 0, ErrNilState
  729. }
  730. channel, err := s.Channel(channelID)
  731. if err != nil {
  732. return
  733. }
  734. guild, err := s.Guild(channel.GuildID)
  735. if err != nil {
  736. return
  737. }
  738. if userID == guild.OwnerID {
  739. apermissions = PermissionAll
  740. return
  741. }
  742. member, err := s.Member(guild.ID, userID)
  743. if err != nil {
  744. return
  745. }
  746. return memberPermissions(guild, channel, member), nil
  747. }
  748. // UserColor returns the color of a user in a channel.
  749. // While colors are defined at a Guild level, determining for a channel is more useful in message handlers.
  750. // 0 is returned in cases of error, which is the color of @everyone.
  751. // userID : The ID of the user to calculate the color for.
  752. // channelID : The ID of the channel to calculate the color for.
  753. func (s *State) UserColor(userID, channelID string) int {
  754. if s == nil {
  755. return 0
  756. }
  757. channel, err := s.Channel(channelID)
  758. if err != nil {
  759. return 0
  760. }
  761. guild, err := s.Guild(channel.GuildID)
  762. if err != nil {
  763. return 0
  764. }
  765. member, err := s.Member(guild.ID, userID)
  766. if err != nil {
  767. return 0
  768. }
  769. roles := Roles(guild.Roles)
  770. sort.Sort(roles)
  771. for _, role := range roles {
  772. for _, roleID := range member.Roles {
  773. if role.ID == roleID {
  774. if role.Color != 0 {
  775. return role.Color
  776. }
  777. }
  778. }
  779. }
  780. return 0
  781. }