message.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. MessageTypeUserPremiumGuildSubscription
  26. MessageTypeUserPremiumGuildSubscriptionTierOne
  27. MessageTypeUserPremiumGuildSubscriptionTierTwo
  28. MessageTypeUserPremiumGuildSubscriptionTierThree
  29. MessageTypeChannelFollowAdd
  30. )
  31. // A Message stores all data related to a specific Discord message.
  32. type Message struct {
  33. // The ID of the message.
  34. ID string `json:"id"`
  35. // The ID of the channel in which the message was sent.
  36. ChannelID string `json:"channel_id"`
  37. // The ID of the guild in which the message was sent.
  38. GuildID string `json:"guild_id,omitempty"`
  39. // The content of the message.
  40. Content string `json:"content"`
  41. // The time at which the messsage was sent.
  42. // CAUTION: this field may be removed in a
  43. // future API version; it is safer to calculate
  44. // the creation time via the ID.
  45. Timestamp Timestamp `json:"timestamp"`
  46. // The time at which the last edit of the message
  47. // occurred, if it has been edited.
  48. EditedTimestamp Timestamp `json:"edited_timestamp"`
  49. // The roles mentioned in the message.
  50. MentionRoles []string `json:"mention_roles"`
  51. // Whether the message is text-to-speech.
  52. TTS bool `json:"tts"`
  53. // Whether the message mentions everyone.
  54. MentionEveryone bool `json:"mention_everyone"`
  55. // The author of the message. This is not guaranteed to be a
  56. // valid user (webhook-sent messages do not possess a full author).
  57. Author *User `json:"author"`
  58. // A list of attachments present in the message.
  59. Attachments []*MessageAttachment `json:"attachments"`
  60. // A list of embeds present in the message. Multiple
  61. // embeds can currently only be sent by webhooks.
  62. Embeds []*MessageEmbed `json:"embeds"`
  63. // A list of users mentioned in the message.
  64. Mentions []*User `json:"mentions"`
  65. // A list of reactions to the message.
  66. Reactions []*MessageReactions `json:"reactions"`
  67. // Whether the message is pinned or not.
  68. Pinned bool `json:"pinned"`
  69. // The type of the message.
  70. Type MessageType `json:"type"`
  71. // The webhook ID of the message, if it was generated by a webhook
  72. WebhookID string `json:"webhook_id"`
  73. // Member properties for this message's author,
  74. // contains only partial information
  75. Member *Member `json:"member"`
  76. // Channels specifically mentioned in this message
  77. // Not all channel mentions in a message will appear in mention_channels.
  78. // Only textual channels that are visible to everyone in a lurkable guild will ever be included.
  79. // Only crossposted messages (via Channel Following) currently include mention_channels at all.
  80. // If no mentions in the message meet these requirements, this field will not be sent.
  81. MentionChannels []*Channel `json:"mention_channels"`
  82. // Is sent with Rich Presence-related chat embeds
  83. Activity *MessageActivity `json:"activity"`
  84. // Is sent with Rich Presence-related chat embeds
  85. Application *MessageApplication `json:"application"`
  86. // MessageReference contains reference data sent with crossposted messages
  87. MessageReference *MessageReference `json:"message_reference"`
  88. // The flags of the message, which describe extra features of a message.
  89. // This is a combination of bit masks; the presence of a certain permission can
  90. // be checked by performing a bitwise AND between this int and the flag.
  91. Flags int `json:"flags"`
  92. }
  93. // File stores info about files you e.g. send in messages.
  94. type File struct {
  95. Name string
  96. ContentType string
  97. Reader io.Reader
  98. }
  99. // MessageSend stores all parameters you can send with ChannelMessageSendComplex.
  100. type MessageSend struct {
  101. Content string `json:"content,omitempty"`
  102. Embed *MessageEmbed `json:"embed,omitempty"`
  103. TTS bool `json:"tts"`
  104. Files []*File `json:"-"`
  105. // TODO: Remove this when compatibility is not required.
  106. File *File `json:"-"`
  107. }
  108. // MessageEdit is used to chain parameters via ChannelMessageEditComplex, which
  109. // is also where you should get the instance from.
  110. type MessageEdit struct {
  111. Content *string `json:"content,omitempty"`
  112. Embed *MessageEmbed `json:"embed,omitempty"`
  113. ID string
  114. Channel string
  115. }
  116. // NewMessageEdit returns a MessageEdit struct, initialized
  117. // with the Channel and ID.
  118. func NewMessageEdit(channelID string, messageID string) *MessageEdit {
  119. return &MessageEdit{
  120. Channel: channelID,
  121. ID: messageID,
  122. }
  123. }
  124. // SetContent is the same as setting the variable Content,
  125. // except it doesn't take a pointer.
  126. func (m *MessageEdit) SetContent(str string) *MessageEdit {
  127. m.Content = &str
  128. return m
  129. }
  130. // SetEmbed is a convenience function for setting the embed,
  131. // so you can chain commands.
  132. func (m *MessageEdit) SetEmbed(embed *MessageEmbed) *MessageEdit {
  133. m.Embed = embed
  134. return m
  135. }
  136. // A MessageAttachment stores data for message attachments.
  137. type MessageAttachment struct {
  138. ID string `json:"id"`
  139. URL string `json:"url"`
  140. ProxyURL string `json:"proxy_url"`
  141. Filename string `json:"filename"`
  142. Width int `json:"width"`
  143. Height int `json:"height"`
  144. Size int `json:"size"`
  145. }
  146. // MessageEmbedFooter is a part of a MessageEmbed struct.
  147. type MessageEmbedFooter struct {
  148. Text string `json:"text,omitempty"`
  149. IconURL string `json:"icon_url,omitempty"`
  150. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  151. }
  152. // MessageEmbedImage is a part of a MessageEmbed struct.
  153. type MessageEmbedImage struct {
  154. URL string `json:"url,omitempty"`
  155. ProxyURL string `json:"proxy_url,omitempty"`
  156. Width int `json:"width,omitempty"`
  157. Height int `json:"height,omitempty"`
  158. }
  159. // MessageEmbedThumbnail is a part of a MessageEmbed struct.
  160. type MessageEmbedThumbnail struct {
  161. URL string `json:"url,omitempty"`
  162. ProxyURL string `json:"proxy_url,omitempty"`
  163. Width int `json:"width,omitempty"`
  164. Height int `json:"height,omitempty"`
  165. }
  166. // MessageEmbedVideo is a part of a MessageEmbed struct.
  167. type MessageEmbedVideo struct {
  168. URL string `json:"url,omitempty"`
  169. ProxyURL string `json:"proxy_url,omitempty"`
  170. Width int `json:"width,omitempty"`
  171. Height int `json:"height,omitempty"`
  172. }
  173. // MessageEmbedProvider is a part of a MessageEmbed struct.
  174. type MessageEmbedProvider struct {
  175. URL string `json:"url,omitempty"`
  176. Name string `json:"name,omitempty"`
  177. }
  178. // MessageEmbedAuthor is a part of a MessageEmbed struct.
  179. type MessageEmbedAuthor struct {
  180. URL string `json:"url,omitempty"`
  181. Name string `json:"name,omitempty"`
  182. IconURL string `json:"icon_url,omitempty"`
  183. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  184. }
  185. // MessageEmbedField is a part of a MessageEmbed struct.
  186. type MessageEmbedField struct {
  187. Name string `json:"name,omitempty"`
  188. Value string `json:"value,omitempty"`
  189. Inline bool `json:"inline,omitempty"`
  190. }
  191. // An MessageEmbed stores data for message embeds.
  192. type MessageEmbed struct {
  193. URL string `json:"url,omitempty"`
  194. Type string `json:"type,omitempty"`
  195. Title string `json:"title,omitempty"`
  196. Description string `json:"description,omitempty"`
  197. Timestamp string `json:"timestamp,omitempty"`
  198. Color int `json:"color,omitempty"`
  199. Footer *MessageEmbedFooter `json:"footer,omitempty"`
  200. Image *MessageEmbedImage `json:"image,omitempty"`
  201. Thumbnail *MessageEmbedThumbnail `json:"thumbnail,omitempty"`
  202. Video *MessageEmbedVideo `json:"video,omitempty"`
  203. Provider *MessageEmbedProvider `json:"provider,omitempty"`
  204. Author *MessageEmbedAuthor `json:"author,omitempty"`
  205. Fields []*MessageEmbedField `json:"fields,omitempty"`
  206. }
  207. // MessageReactions holds a reactions object for a message.
  208. type MessageReactions struct {
  209. Count int `json:"count"`
  210. Me bool `json:"me"`
  211. Emoji *Emoji `json:"emoji"`
  212. }
  213. // MessageActivity is sent with Rich Presence-related chat embeds
  214. type MessageActivity struct {
  215. Type MessageActivityType `json:"type"`
  216. PartyID string `json:"party_id"`
  217. }
  218. // MessageActivityType is the type of message activity
  219. type MessageActivityType int
  220. // Constants for the different types of Message Activity
  221. const (
  222. MessageActivityTypeJoin = iota + 1
  223. MessageActivityTypeSpectate
  224. MessageActivityTypeListen
  225. MessageActivityTypeJoinRequest
  226. )
  227. // MessageFlag describes an extra feature of the message
  228. type MessageFlag int
  229. // Constants for the different bit offsets of Message Flags
  230. const (
  231. // This message has been published to subscribed channels (via Channel Following)
  232. MessageFlagCrossposted = 1 << iota
  233. // This message originated from a message in another channel (via Channel Following)
  234. MessageFlagIsCrosspost
  235. // Do not include any embeds when serializing this message
  236. MessageFlagSuppressEmbeds
  237. )
  238. // MessageApplication is sent with Rich Presence-related chat embeds
  239. type MessageApplication struct {
  240. ID string `json:"id"`
  241. CoverImage string `json:"cover_image"`
  242. Description string `json:"description"`
  243. Icon string `json:"icon"`
  244. Name string `json:"name"`
  245. }
  246. // MessageReference contains reference data sent with crossposted messages
  247. type MessageReference struct {
  248. MessageID string `json:"message_id"`
  249. ChannelID string `json:"channel_id"`
  250. GuildID string `json:"guild_id"`
  251. }
  252. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  253. // username of the mention.
  254. func (m *Message) ContentWithMentionsReplaced() (content string) {
  255. content = m.Content
  256. for _, user := range m.Mentions {
  257. content = strings.NewReplacer(
  258. "<@"+user.ID+">", "@"+user.Username,
  259. "<@!"+user.ID+">", "@"+user.Username,
  260. ).Replace(content)
  261. }
  262. return
  263. }
  264. var patternChannels = regexp.MustCompile("<#[^>]*>")
  265. // ContentWithMoreMentionsReplaced will replace all @<id> mentions with the
  266. // username of the mention, but also role IDs and more.
  267. func (m *Message) ContentWithMoreMentionsReplaced(s *Session) (content string, err error) {
  268. content = m.Content
  269. if !s.StateEnabled {
  270. content = m.ContentWithMentionsReplaced()
  271. return
  272. }
  273. channel, err := s.State.Channel(m.ChannelID)
  274. if err != nil {
  275. content = m.ContentWithMentionsReplaced()
  276. return
  277. }
  278. for _, user := range m.Mentions {
  279. nick := user.Username
  280. member, err := s.State.Member(channel.GuildID, user.ID)
  281. if err == nil && member.Nick != "" {
  282. nick = member.Nick
  283. }
  284. content = strings.NewReplacer(
  285. "<@"+user.ID+">", "@"+user.Username,
  286. "<@!"+user.ID+">", "@"+nick,
  287. ).Replace(content)
  288. }
  289. for _, roleID := range m.MentionRoles {
  290. role, err := s.State.Role(channel.GuildID, roleID)
  291. if err != nil || !role.Mentionable {
  292. continue
  293. }
  294. content = strings.Replace(content, "<@&"+role.ID+">", "@"+role.Name, -1)
  295. }
  296. content = patternChannels.ReplaceAllStringFunc(content, func(mention string) string {
  297. channel, err := s.State.Channel(mention[2 : len(mention)-1])
  298. if err != nil || channel.Type == ChannelTypeGuildVoice {
  299. return mention
  300. }
  301. return "#" + channel.Name
  302. })
  303. return
  304. }