message.go 8.8 KB

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