message.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. // The type of the message.
  63. Type MessageType `json:"type"`
  64. }
  65. // File stores info about files you e.g. send in messages.
  66. type File struct {
  67. Name string
  68. ContentType string
  69. Reader io.Reader
  70. }
  71. // MessageSend stores all parameters you can send with ChannelMessageSendComplex.
  72. type MessageSend struct {
  73. Content string `json:"content,omitempty"`
  74. Embed *MessageEmbed `json:"embed,omitempty"`
  75. Tts bool `json:"tts"`
  76. Files []*File `json:"-"`
  77. // TODO: Remove this when compatibility is not required.
  78. File *File `json:"-"`
  79. }
  80. // MessageEdit is used to chain parameters via ChannelMessageEditComplex, which
  81. // is also where you should get the instance from.
  82. type MessageEdit struct {
  83. Content *string `json:"content,omitempty"`
  84. Embed *MessageEmbed `json:"embed,omitempty"`
  85. ID string
  86. Channel string
  87. }
  88. // NewMessageEdit returns a MessageEdit struct, initialized
  89. // with the Channel and ID.
  90. func NewMessageEdit(channelID string, messageID string) *MessageEdit {
  91. return &MessageEdit{
  92. Channel: channelID,
  93. ID: messageID,
  94. }
  95. }
  96. // SetContent is the same as setting the variable Content,
  97. // except it doesn't take a pointer.
  98. func (m *MessageEdit) SetContent(str string) *MessageEdit {
  99. m.Content = &str
  100. return m
  101. }
  102. // SetEmbed is a convenience function for setting the embed,
  103. // so you can chain commands.
  104. func (m *MessageEdit) SetEmbed(embed *MessageEmbed) *MessageEdit {
  105. m.Embed = embed
  106. return m
  107. }
  108. // A MessageAttachment stores data for message attachments.
  109. type MessageAttachment struct {
  110. ID string `json:"id"`
  111. URL string `json:"url"`
  112. ProxyURL string `json:"proxy_url"`
  113. Filename string `json:"filename"`
  114. Width int `json:"width"`
  115. Height int `json:"height"`
  116. Size int `json:"size"`
  117. }
  118. // MessageEmbedFooter is a part of a MessageEmbed struct.
  119. type MessageEmbedFooter struct {
  120. Text string `json:"text,omitempty"`
  121. IconURL string `json:"icon_url,omitempty"`
  122. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  123. }
  124. // MessageEmbedImage is a part of a MessageEmbed struct.
  125. type MessageEmbedImage struct {
  126. URL string `json:"url,omitempty"`
  127. ProxyURL string `json:"proxy_url,omitempty"`
  128. Width int `json:"width,omitempty"`
  129. Height int `json:"height,omitempty"`
  130. }
  131. // MessageEmbedThumbnail is a part of a MessageEmbed struct.
  132. type MessageEmbedThumbnail struct {
  133. URL string `json:"url,omitempty"`
  134. ProxyURL string `json:"proxy_url,omitempty"`
  135. Width int `json:"width,omitempty"`
  136. Height int `json:"height,omitempty"`
  137. }
  138. // MessageEmbedVideo is a part of a MessageEmbed struct.
  139. type MessageEmbedVideo struct {
  140. URL string `json:"url,omitempty"`
  141. ProxyURL string `json:"proxy_url,omitempty"`
  142. Width int `json:"width,omitempty"`
  143. Height int `json:"height,omitempty"`
  144. }
  145. // MessageEmbedProvider is a part of a MessageEmbed struct.
  146. type MessageEmbedProvider struct {
  147. URL string `json:"url,omitempty"`
  148. Name string `json:"name,omitempty"`
  149. }
  150. // MessageEmbedAuthor is a part of a MessageEmbed struct.
  151. type MessageEmbedAuthor struct {
  152. URL string `json:"url,omitempty"`
  153. Name string `json:"name,omitempty"`
  154. IconURL string `json:"icon_url,omitempty"`
  155. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  156. }
  157. // MessageEmbedField is a part of a MessageEmbed struct.
  158. type MessageEmbedField struct {
  159. Name string `json:"name,omitempty"`
  160. Value string `json:"value,omitempty"`
  161. Inline bool `json:"inline,omitempty"`
  162. }
  163. // An MessageEmbed stores data for message embeds.
  164. type MessageEmbed struct {
  165. URL string `json:"url,omitempty"`
  166. Type string `json:"type,omitempty"`
  167. Title string `json:"title,omitempty"`
  168. Description string `json:"description,omitempty"`
  169. Timestamp string `json:"timestamp,omitempty"`
  170. Color int `json:"color,omitempty"`
  171. Footer *MessageEmbedFooter `json:"footer,omitempty"`
  172. Image *MessageEmbedImage `json:"image,omitempty"`
  173. Thumbnail *MessageEmbedThumbnail `json:"thumbnail,omitempty"`
  174. Video *MessageEmbedVideo `json:"video,omitempty"`
  175. Provider *MessageEmbedProvider `json:"provider,omitempty"`
  176. Author *MessageEmbedAuthor `json:"author,omitempty"`
  177. Fields []*MessageEmbedField `json:"fields,omitempty"`
  178. }
  179. // MessageReactions holds a reactions object for a message.
  180. type MessageReactions struct {
  181. Count int `json:"count"`
  182. Me bool `json:"me"`
  183. Emoji *Emoji `json:"emoji"`
  184. }
  185. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  186. // username of the mention.
  187. func (m *Message) ContentWithMentionsReplaced() (content string) {
  188. content = m.Content
  189. for _, user := range m.Mentions {
  190. content = strings.NewReplacer(
  191. "<@"+user.ID+">", "@"+user.Username,
  192. "<@!"+user.ID+">", "@"+user.Username,
  193. ).Replace(content)
  194. }
  195. return
  196. }
  197. var patternChannels = regexp.MustCompile("<#[^>]*>")
  198. // ContentWithMoreMentionsReplaced will replace all @<id> mentions with the
  199. // username of the mention, but also role IDs and more.
  200. func (m *Message) ContentWithMoreMentionsReplaced(s *Session) (content string, err error) {
  201. content = m.Content
  202. if !s.StateEnabled {
  203. content = m.ContentWithMentionsReplaced()
  204. return
  205. }
  206. channel, err := s.State.Channel(m.ChannelID)
  207. if err != nil {
  208. content = m.ContentWithMentionsReplaced()
  209. return
  210. }
  211. for _, user := range m.Mentions {
  212. nick := user.Username
  213. member, err := s.State.Member(channel.GuildID, user.ID)
  214. if err == nil && member.Nick != "" {
  215. nick = member.Nick
  216. }
  217. content = strings.NewReplacer(
  218. "<@"+user.ID+">", "@"+user.Username,
  219. "<@!"+user.ID+">", "@"+nick,
  220. ).Replace(content)
  221. }
  222. for _, roleID := range m.MentionRoles {
  223. role, err := s.State.Role(channel.GuildID, roleID)
  224. if err != nil || !role.Mentionable {
  225. continue
  226. }
  227. content = strings.Replace(content, "<@&"+role.ID+">", "@"+role.Name, -1)
  228. }
  229. content = patternChannels.ReplaceAllStringFunc(content, func(mention string) string {
  230. channel, err := s.State.Channel(mention[2 : len(mention)-1])
  231. if err != nil || channel.Type == ChannelTypeGuildVoice {
  232. return mention
  233. }
  234. return "#" + channel.Name
  235. })
  236. return
  237. }