message.go 8.3 KB

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