message.go 7.5 KB

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