message.go 8.6 KB

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