message.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 the Message struct
  7. package discordgo
  8. import (
  9. "io"
  10. "regexp"
  11. "strings"
  12. )
  13. // A Message stores all data related to a specific Discord message.
  14. type Message struct {
  15. ID string `json:"id"`
  16. ChannelID string `json:"channel_id"`
  17. Content string `json:"content"`
  18. Timestamp Timestamp `json:"timestamp"`
  19. EditedTimestamp Timestamp `json:"edited_timestamp"`
  20. MentionRoles []string `json:"mention_roles"`
  21. Tts bool `json:"tts"`
  22. MentionEveryone bool `json:"mention_everyone"`
  23. Author *User `json:"author"`
  24. Attachments []*MessageAttachment `json:"attachments"`
  25. Embeds []*MessageEmbed `json:"embeds"`
  26. Mentions []*User `json:"mentions"`
  27. Reactions []*MessageReactions `json:"reactions"`
  28. }
  29. // File stores info about files you e.g. send in messages.
  30. type File struct {
  31. Name string
  32. ContentType string
  33. Reader io.Reader
  34. }
  35. // MessageSend stores all parameters you can send with ChannelMessageSendComplex.
  36. type MessageSend struct {
  37. Content string `json:"content,omitempty"`
  38. Embed *MessageEmbed `json:"embed,omitempty"`
  39. Tts bool `json:"tts"`
  40. Files []*File `json:"-"`
  41. // TODO: Remove this when compatibility is not required.
  42. File *File `json:"-"`
  43. }
  44. // MessageEdit is used to chain parameters via ChannelMessageEditComplex, which
  45. // is also where you should get the instance from.
  46. type MessageEdit struct {
  47. Content *string `json:"content,omitempty"`
  48. Embed *MessageEmbed `json:"embed,omitempty"`
  49. ID string
  50. Channel string
  51. }
  52. // NewMessageEdit returns a MessageEdit struct, initialized
  53. // with the Channel and ID.
  54. func NewMessageEdit(channelID string, messageID string) *MessageEdit {
  55. return &MessageEdit{
  56. Channel: channelID,
  57. ID: messageID,
  58. }
  59. }
  60. // SetContent is the same as setting the variable Content,
  61. // except it doesn't take a pointer.
  62. func (m *MessageEdit) SetContent(str string) *MessageEdit {
  63. m.Content = &str
  64. return m
  65. }
  66. // SetEmbed is a convenience function for setting the embed,
  67. // so you can chain commands.
  68. func (m *MessageEdit) SetEmbed(embed *MessageEmbed) *MessageEdit {
  69. m.Embed = embed
  70. return m
  71. }
  72. // A MessageAttachment stores data for message attachments.
  73. type MessageAttachment struct {
  74. ID string `json:"id"`
  75. URL string `json:"url"`
  76. ProxyURL string `json:"proxy_url"`
  77. Filename string `json:"filename"`
  78. Width int `json:"width"`
  79. Height int `json:"height"`
  80. Size int `json:"size"`
  81. }
  82. // MessageEmbedFooter is a part of a MessageEmbed struct.
  83. type MessageEmbedFooter struct {
  84. Text string `json:"text,omitempty"`
  85. IconURL string `json:"icon_url,omitempty"`
  86. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  87. }
  88. // MessageEmbedImage is a part of a MessageEmbed struct.
  89. type MessageEmbedImage struct {
  90. URL string `json:"url,omitempty"`
  91. ProxyURL string `json:"proxy_url,omitempty"`
  92. Width int `json:"width,omitempty"`
  93. Height int `json:"height,omitempty"`
  94. }
  95. // MessageEmbedThumbnail is a part of a MessageEmbed struct.
  96. type MessageEmbedThumbnail struct {
  97. URL string `json:"url,omitempty"`
  98. ProxyURL string `json:"proxy_url,omitempty"`
  99. Width int `json:"width,omitempty"`
  100. Height int `json:"height,omitempty"`
  101. }
  102. // MessageEmbedVideo is a part of a MessageEmbed struct.
  103. type MessageEmbedVideo struct {
  104. URL string `json:"url,omitempty"`
  105. ProxyURL string `json:"proxy_url,omitempty"`
  106. Width int `json:"width,omitempty"`
  107. Height int `json:"height,omitempty"`
  108. }
  109. // MessageEmbedProvider is a part of a MessageEmbed struct.
  110. type MessageEmbedProvider struct {
  111. URL string `json:"url,omitempty"`
  112. Name string `json:"name,omitempty"`
  113. }
  114. // MessageEmbedAuthor is a part of a MessageEmbed struct.
  115. type MessageEmbedAuthor struct {
  116. URL string `json:"url,omitempty"`
  117. Name string `json:"name,omitempty"`
  118. IconURL string `json:"icon_url,omitempty"`
  119. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  120. }
  121. // MessageEmbedField is a part of a MessageEmbed struct.
  122. type MessageEmbedField struct {
  123. Name string `json:"name,omitempty"`
  124. Value string `json:"value,omitempty"`
  125. Inline bool `json:"inline,omitempty"`
  126. }
  127. // An MessageEmbed stores data for message embeds.
  128. type MessageEmbed struct {
  129. URL string `json:"url,omitempty"`
  130. Type string `json:"type,omitempty"`
  131. Title string `json:"title,omitempty"`
  132. Description string `json:"description,omitempty"`
  133. Timestamp string `json:"timestamp,omitempty"`
  134. Color int `json:"color,omitempty"`
  135. Footer *MessageEmbedFooter `json:"footer,omitempty"`
  136. Image *MessageEmbedImage `json:"image,omitempty"`
  137. Thumbnail *MessageEmbedThumbnail `json:"thumbnail,omitempty"`
  138. Video *MessageEmbedVideo `json:"video,omitempty"`
  139. Provider *MessageEmbedProvider `json:"provider,omitempty"`
  140. Author *MessageEmbedAuthor `json:"author,omitempty"`
  141. Fields []*MessageEmbedField `json:"fields,omitempty"`
  142. }
  143. // MessageReactions holds a reactions object for a message.
  144. type MessageReactions struct {
  145. Count int `json:"count"`
  146. Me bool `json:"me"`
  147. Emoji *Emoji `json:"emoji"`
  148. }
  149. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  150. // username of the mention.
  151. func (m *Message) ContentWithMentionsReplaced() (content string) {
  152. content = m.Content
  153. for _, user := range m.Mentions {
  154. content = strings.NewReplacer(
  155. "<@"+user.ID+">", "@"+user.Username,
  156. "<@!"+user.ID+">", "@"+user.Username,
  157. ).Replace(content)
  158. }
  159. return
  160. }
  161. var patternChannels = regexp.MustCompile("<#[^>]*>")
  162. // ContentWithMoreMentionsReplaced will replace all @<id> mentions with the
  163. // username of the mention, but also role IDs and more.
  164. func (m *Message) ContentWithMoreMentionsReplaced(s *Session) (content string, err error) {
  165. content = m.Content
  166. if !s.StateEnabled {
  167. content = m.ContentWithMentionsReplaced()
  168. return
  169. }
  170. channel, err := s.State.Channel(m.ChannelID)
  171. if err != nil {
  172. content = m.ContentWithMentionsReplaced()
  173. return
  174. }
  175. for _, user := range m.Mentions {
  176. nick := user.Username
  177. member, err := s.State.Member(channel.GuildID, user.ID)
  178. if err == nil && member.Nick != "" {
  179. nick = member.Nick
  180. }
  181. content = strings.NewReplacer(
  182. "<@"+user.ID+">", "@"+user.Username,
  183. "<@!"+user.ID+">", "@"+nick,
  184. ).Replace(content)
  185. }
  186. for _, roleID := range m.MentionRoles {
  187. role, err := s.State.Role(channel.GuildID, roleID)
  188. if err != nil || !role.Mentionable {
  189. continue
  190. }
  191. content = strings.Replace(content, "<&"+role.ID+">", "@"+role.Name, -1)
  192. }
  193. content = patternChannels.ReplaceAllStringFunc(content, func(mention string) string {
  194. channel, err := s.State.Channel(mention[2 : len(mention)-1])
  195. if err != nil || channel.Type == "voice" {
  196. return mention
  197. }
  198. return "#" + channel.Name
  199. })
  200. return
  201. }