message.go 8.5 KB

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