message.go 7.6 KB

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