message.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // MessageType is the type of Message
  14. type MessageType int
  15. // Block contains the valid known MessageType values
  16. const (
  17. MessageTypeDefault MessageType = iota
  18. MessageTypeRecipientAdd
  19. MessageTypeRecipientRemove
  20. MessageTypeCall
  21. MessageTypeChannelNameChange
  22. MessageTypeChannelIconChange
  23. MessageTypeChannelPinnedMessage
  24. MessageTypeGuildMemberJoin
  25. MessageTypeUserPremiumGuildSubscription
  26. MessageTypeUserPremiumGuildSubscriptionTierOne
  27. MessageTypeUserPremiumGuildSubscriptionTierTwo
  28. MessageTypeUserPremiumGuildSubscriptionTierThree
  29. )
  30. // A Message stores all data related to a specific Discord message.
  31. type Message struct {
  32. // The ID of the message.
  33. ID string `json:"id"`
  34. // The ID of the channel in which the message was sent.
  35. ChannelID string `json:"channel_id"`
  36. // The ID of the guild in which the message was sent.
  37. GuildID string `json:"guild_id,omitempty"`
  38. // The content of the message.
  39. Content string `json:"content"`
  40. // The time at which the messsage was sent.
  41. // CAUTION: this field may be removed in a
  42. // future API version; it is safer to calculate
  43. // the creation time via the ID.
  44. Timestamp Timestamp `json:"timestamp"`
  45. // The time at which the last edit of the message
  46. // occurred, if it has been edited.
  47. EditedTimestamp Timestamp `json:"edited_timestamp"`
  48. // The roles mentioned in the message.
  49. MentionRoles []string `json:"mention_roles"`
  50. // Whether the message is text-to-speech.
  51. Tts bool `json:"tts"`
  52. // Whether the message mentions everyone.
  53. MentionEveryone bool `json:"mention_everyone"`
  54. // The author of the message. This is not guaranteed to be a
  55. // valid user (webhook-sent messages do not possess a full author).
  56. Author *User `json:"author"`
  57. // A list of attachments present in the message.
  58. Attachments []*MessageAttachment `json:"attachments"`
  59. // A list of embeds present in the message. Multiple
  60. // embeds can currently only be sent by webhooks.
  61. Embeds []*MessageEmbed `json:"embeds"`
  62. // A list of users mentioned in the message.
  63. Mentions []*User `json:"mentions"`
  64. // A list of reactions to the message.
  65. Reactions []*MessageReactions `json:"reactions"`
  66. // Whether the message is pinned or not.
  67. Pinned bool `json:"pinned"`
  68. // The type of the message.
  69. Type MessageType `json:"type"`
  70. // The webhook ID of the message, if it was generated by a webhook
  71. WebhookID string `json:"webhook_id"`
  72. }
  73. // File stores info about files you e.g. send in messages.
  74. type File struct {
  75. Name string
  76. ContentType string
  77. Reader io.Reader
  78. }
  79. // MessageSend stores all parameters you can send with ChannelMessageSendComplex.
  80. type MessageSend struct {
  81. Content string `json:"content,omitempty"`
  82. Embed *MessageEmbed `json:"embed,omitempty"`
  83. Tts bool `json:"tts"`
  84. Files []*File `json:"-"`
  85. // TODO: Remove this when compatibility is not required.
  86. File *File `json:"-"`
  87. }
  88. // MessageEdit is used to chain parameters via ChannelMessageEditComplex, which
  89. // is also where you should get the instance from.
  90. type MessageEdit struct {
  91. Content *string `json:"content,omitempty"`
  92. Embed *MessageEmbed `json:"embed,omitempty"`
  93. ID string
  94. Channel string
  95. }
  96. // NewMessageEdit returns a MessageEdit struct, initialized
  97. // with the Channel and ID.
  98. func NewMessageEdit(channelID string, messageID string) *MessageEdit {
  99. return &MessageEdit{
  100. Channel: channelID,
  101. ID: messageID,
  102. }
  103. }
  104. // SetContent is the same as setting the variable Content,
  105. // except it doesn't take a pointer.
  106. func (m *MessageEdit) SetContent(str string) *MessageEdit {
  107. m.Content = &str
  108. return m
  109. }
  110. // SetEmbed is a convenience function for setting the embed,
  111. // so you can chain commands.
  112. func (m *MessageEdit) SetEmbed(embed *MessageEmbed) *MessageEdit {
  113. m.Embed = embed
  114. return m
  115. }
  116. // A MessageAttachment stores data for message attachments.
  117. type MessageAttachment struct {
  118. ID string `json:"id"`
  119. URL string `json:"url"`
  120. ProxyURL string `json:"proxy_url"`
  121. Filename string `json:"filename"`
  122. Width int `json:"width"`
  123. Height int `json:"height"`
  124. Size int `json:"size"`
  125. }
  126. // MessageEmbedFooter is a part of a MessageEmbed struct.
  127. type MessageEmbedFooter struct {
  128. Text string `json:"text,omitempty"`
  129. IconURL string `json:"icon_url,omitempty"`
  130. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  131. }
  132. // MessageEmbedImage is a part of a MessageEmbed struct.
  133. type MessageEmbedImage struct {
  134. URL string `json:"url,omitempty"`
  135. ProxyURL string `json:"proxy_url,omitempty"`
  136. Width int `json:"width,omitempty"`
  137. Height int `json:"height,omitempty"`
  138. }
  139. // MessageEmbedThumbnail is a part of a MessageEmbed struct.
  140. type MessageEmbedThumbnail struct {
  141. URL string `json:"url,omitempty"`
  142. ProxyURL string `json:"proxy_url,omitempty"`
  143. Width int `json:"width,omitempty"`
  144. Height int `json:"height,omitempty"`
  145. }
  146. // MessageEmbedVideo is a part of a MessageEmbed struct.
  147. type MessageEmbedVideo struct {
  148. URL string `json:"url,omitempty"`
  149. ProxyURL string `json:"proxy_url,omitempty"`
  150. Width int `json:"width,omitempty"`
  151. Height int `json:"height,omitempty"`
  152. }
  153. // MessageEmbedProvider is a part of a MessageEmbed struct.
  154. type MessageEmbedProvider struct {
  155. URL string `json:"url,omitempty"`
  156. Name string `json:"name,omitempty"`
  157. }
  158. // MessageEmbedAuthor is a part of a MessageEmbed struct.
  159. type MessageEmbedAuthor struct {
  160. URL string `json:"url,omitempty"`
  161. Name string `json:"name,omitempty"`
  162. IconURL string `json:"icon_url,omitempty"`
  163. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  164. }
  165. // MessageEmbedField is a part of a MessageEmbed struct.
  166. type MessageEmbedField struct {
  167. Name string `json:"name,omitempty"`
  168. Value string `json:"value,omitempty"`
  169. Inline bool `json:"inline,omitempty"`
  170. }
  171. // An MessageEmbed stores data for message embeds.
  172. type MessageEmbed struct {
  173. URL string `json:"url,omitempty"`
  174. Type string `json:"type,omitempty"`
  175. Title string `json:"title,omitempty"`
  176. Description string `json:"description,omitempty"`
  177. Timestamp string `json:"timestamp,omitempty"`
  178. Color int `json:"color,omitempty"`
  179. Footer *MessageEmbedFooter `json:"footer,omitempty"`
  180. Image *MessageEmbedImage `json:"image,omitempty"`
  181. Thumbnail *MessageEmbedThumbnail `json:"thumbnail,omitempty"`
  182. Video *MessageEmbedVideo `json:"video,omitempty"`
  183. Provider *MessageEmbedProvider `json:"provider,omitempty"`
  184. Author *MessageEmbedAuthor `json:"author,omitempty"`
  185. Fields []*MessageEmbedField `json:"fields,omitempty"`
  186. }
  187. // MessageReactions holds a reactions object for a message.
  188. type MessageReactions struct {
  189. Count int `json:"count"`
  190. Me bool `json:"me"`
  191. Emoji *Emoji `json:"emoji"`
  192. }
  193. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  194. // username of the mention.
  195. func (m *Message) ContentWithMentionsReplaced() (content string) {
  196. content = m.Content
  197. for _, user := range m.Mentions {
  198. content = strings.NewReplacer(
  199. "<@"+user.ID+">", "@"+user.Username,
  200. "<@!"+user.ID+">", "@"+user.Username,
  201. ).Replace(content)
  202. }
  203. return
  204. }
  205. var patternChannels = regexp.MustCompile("<#[^>]*>")
  206. // ContentWithMoreMentionsReplaced will replace all @<id> mentions with the
  207. // username of the mention, but also role IDs and more.
  208. func (m *Message) ContentWithMoreMentionsReplaced(s *Session) (content string, err error) {
  209. content = m.Content
  210. if !s.StateEnabled {
  211. content = m.ContentWithMentionsReplaced()
  212. return
  213. }
  214. channel, err := s.State.Channel(m.ChannelID)
  215. if err != nil {
  216. content = m.ContentWithMentionsReplaced()
  217. return
  218. }
  219. for _, user := range m.Mentions {
  220. nick := user.Username
  221. member, err := s.State.Member(channel.GuildID, user.ID)
  222. if err == nil && member.Nick != "" {
  223. nick = member.Nick
  224. }
  225. content = strings.NewReplacer(
  226. "<@"+user.ID+">", "@"+user.Username,
  227. "<@!"+user.ID+">", "@"+nick,
  228. ).Replace(content)
  229. }
  230. for _, roleID := range m.MentionRoles {
  231. role, err := s.State.Role(channel.GuildID, roleID)
  232. if err != nil || !role.Mentionable {
  233. continue
  234. }
  235. content = strings.Replace(content, "<@&"+role.ID+">", "@"+role.Name, -1)
  236. }
  237. content = patternChannels.ReplaceAllStringFunc(content, func(mention string) string {
  238. channel, err := s.State.Channel(mention[2 : len(mention)-1])
  239. if err != nil || channel.Type == ChannelTypeGuildVoice {
  240. return mention
  241. }
  242. return "#" + channel.Name
  243. })
  244. return
  245. }