message.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. AllowedMentions *MessageAllowedMentions `json:"allowed_mentions,omitempty"`
  106. // TODO: Remove this when compatibility is not required.
  107. File *File `json:"-"`
  108. }
  109. // MessageEdit is used to chain parameters via ChannelMessageEditComplex, which
  110. // is also where you should get the instance from.
  111. type MessageEdit struct {
  112. Content *string `json:"content,omitempty"`
  113. Embed *MessageEmbed `json:"embed,omitempty"`
  114. AllowedMentions *MessageAllowedMentions `json:"allowed_mentions,omitempty"`
  115. ID string
  116. Channel string
  117. }
  118. // NewMessageEdit returns a MessageEdit struct, initialized
  119. // with the Channel and ID.
  120. func NewMessageEdit(channelID string, messageID string) *MessageEdit {
  121. return &MessageEdit{
  122. Channel: channelID,
  123. ID: messageID,
  124. }
  125. }
  126. // SetContent is the same as setting the variable Content,
  127. // except it doesn't take a pointer.
  128. func (m *MessageEdit) SetContent(str string) *MessageEdit {
  129. m.Content = &str
  130. return m
  131. }
  132. // SetEmbed is a convenience function for setting the embed,
  133. // so you can chain commands.
  134. func (m *MessageEdit) SetEmbed(embed *MessageEmbed) *MessageEdit {
  135. m.Embed = embed
  136. return m
  137. }
  138. // AllowedMentionType describes the types of mentions used
  139. // in the MessageAllowedMentions type.
  140. type AllowedMentionType string
  141. // The types of mentions used in MessageAllowedMentions.
  142. const (
  143. AllowedMentionTypeRoles AllowedMentionType = "roles"
  144. AllowedMentionTypeUsers AllowedMentionType = "users"
  145. AllowedMentionTypeEveryone AllowedMentionType = "everyone"
  146. )
  147. // MessageAllowedMentions allows the user to specify which mentions
  148. // Discord is allowed to parse in this message. This is useful when
  149. // sending user input as a message, as it prevents unwanted mentions.
  150. // If this type is used, all mentions must be explicitly whitelisted,
  151. // either by putting an AllowedMentionType in the Parse slice
  152. // (allowing all mentions of that type) or, in the case of roles and
  153. // users, explicitly allowing those mentions on an ID-by-ID basis.
  154. // For more information on this functionality, see:
  155. // https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object-allowed-mentions-reference
  156. type MessageAllowedMentions struct {
  157. // The mention types that are allowed to be parsed in this message.
  158. // Please note that this is purposely **not** marked as omitempty,
  159. // so if a zero-value MessageAllowedMentions object is provided no
  160. // mentions will be allowed.
  161. Parse []AllowedMentionType `json:"parse"`
  162. // A list of role IDs to allow. This cannot be used when specifying
  163. // AllowedMentionTypeRoles in the Parse slice.
  164. Roles []string `json:"roles,omitempty"`
  165. // A list of user IDs to allow. This cannot be used when specifying
  166. // AllowedMentionTypeUsers in the Parse slice.
  167. Users []string `json:"users,omitempty"`
  168. }
  169. // A MessageAttachment stores data for message attachments.
  170. type MessageAttachment struct {
  171. ID string `json:"id"`
  172. URL string `json:"url"`
  173. ProxyURL string `json:"proxy_url"`
  174. Filename string `json:"filename"`
  175. Width int `json:"width"`
  176. Height int `json:"height"`
  177. Size int `json:"size"`
  178. }
  179. // MessageEmbedFooter is a part of a MessageEmbed struct.
  180. type MessageEmbedFooter struct {
  181. Text string `json:"text,omitempty"`
  182. IconURL string `json:"icon_url,omitempty"`
  183. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  184. }
  185. // MessageEmbedImage is a part of a MessageEmbed struct.
  186. type MessageEmbedImage struct {
  187. URL string `json:"url,omitempty"`
  188. ProxyURL string `json:"proxy_url,omitempty"`
  189. Width int `json:"width,omitempty"`
  190. Height int `json:"height,omitempty"`
  191. }
  192. // MessageEmbedThumbnail is a part of a MessageEmbed struct.
  193. type MessageEmbedThumbnail struct {
  194. URL string `json:"url,omitempty"`
  195. ProxyURL string `json:"proxy_url,omitempty"`
  196. Width int `json:"width,omitempty"`
  197. Height int `json:"height,omitempty"`
  198. }
  199. // MessageEmbedVideo is a part of a MessageEmbed struct.
  200. type MessageEmbedVideo struct {
  201. URL string `json:"url,omitempty"`
  202. ProxyURL string `json:"proxy_url,omitempty"`
  203. Width int `json:"width,omitempty"`
  204. Height int `json:"height,omitempty"`
  205. }
  206. // MessageEmbedProvider is a part of a MessageEmbed struct.
  207. type MessageEmbedProvider struct {
  208. URL string `json:"url,omitempty"`
  209. Name string `json:"name,omitempty"`
  210. }
  211. // MessageEmbedAuthor is a part of a MessageEmbed struct.
  212. type MessageEmbedAuthor struct {
  213. URL string `json:"url,omitempty"`
  214. Name string `json:"name,omitempty"`
  215. IconURL string `json:"icon_url,omitempty"`
  216. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  217. }
  218. // MessageEmbedField is a part of a MessageEmbed struct.
  219. type MessageEmbedField struct {
  220. Name string `json:"name,omitempty"`
  221. Value string `json:"value,omitempty"`
  222. Inline bool `json:"inline,omitempty"`
  223. }
  224. // An MessageEmbed stores data for message embeds.
  225. type MessageEmbed struct {
  226. URL string `json:"url,omitempty"`
  227. Type string `json:"type,omitempty"`
  228. Title string `json:"title,omitempty"`
  229. Description string `json:"description,omitempty"`
  230. Timestamp string `json:"timestamp,omitempty"`
  231. Color int `json:"color,omitempty"`
  232. Footer *MessageEmbedFooter `json:"footer,omitempty"`
  233. Image *MessageEmbedImage `json:"image,omitempty"`
  234. Thumbnail *MessageEmbedThumbnail `json:"thumbnail,omitempty"`
  235. Video *MessageEmbedVideo `json:"video,omitempty"`
  236. Provider *MessageEmbedProvider `json:"provider,omitempty"`
  237. Author *MessageEmbedAuthor `json:"author,omitempty"`
  238. Fields []*MessageEmbedField `json:"fields,omitempty"`
  239. }
  240. // MessageReactions holds a reactions object for a message.
  241. type MessageReactions struct {
  242. Count int `json:"count"`
  243. Me bool `json:"me"`
  244. Emoji *Emoji `json:"emoji"`
  245. }
  246. // MessageActivity is sent with Rich Presence-related chat embeds
  247. type MessageActivity struct {
  248. Type MessageActivityType `json:"type"`
  249. PartyID string `json:"party_id"`
  250. }
  251. // MessageActivityType is the type of message activity
  252. type MessageActivityType int
  253. // Constants for the different types of Message Activity
  254. const (
  255. MessageActivityTypeJoin = iota + 1
  256. MessageActivityTypeSpectate
  257. MessageActivityTypeListen
  258. MessageActivityTypeJoinRequest
  259. )
  260. // MessageFlag describes an extra feature of the message
  261. type MessageFlag int
  262. // Constants for the different bit offsets of Message Flags
  263. const (
  264. // This message has been published to subscribed channels (via Channel Following)
  265. MessageFlagCrossposted = 1 << iota
  266. // This message originated from a message in another channel (via Channel Following)
  267. MessageFlagIsCrosspost
  268. // Do not include any embeds when serializing this message
  269. MessageFlagSuppressEmbeds
  270. )
  271. // MessageApplication is sent with Rich Presence-related chat embeds
  272. type MessageApplication struct {
  273. ID string `json:"id"`
  274. CoverImage string `json:"cover_image"`
  275. Description string `json:"description"`
  276. Icon string `json:"icon"`
  277. Name string `json:"name"`
  278. }
  279. // MessageReference contains reference data sent with crossposted messages
  280. type MessageReference struct {
  281. MessageID string `json:"message_id"`
  282. ChannelID string `json:"channel_id"`
  283. GuildID string `json:"guild_id"`
  284. }
  285. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  286. // username of the mention.
  287. func (m *Message) ContentWithMentionsReplaced() (content string) {
  288. content = m.Content
  289. for _, user := range m.Mentions {
  290. content = strings.NewReplacer(
  291. "<@"+user.ID+">", "@"+user.Username,
  292. "<@!"+user.ID+">", "@"+user.Username,
  293. ).Replace(content)
  294. }
  295. return
  296. }
  297. var patternChannels = regexp.MustCompile("<#[^>]*>")
  298. // ContentWithMoreMentionsReplaced will replace all @<id> mentions with the
  299. // username of the mention, but also role IDs and more.
  300. func (m *Message) ContentWithMoreMentionsReplaced(s *Session) (content string, err error) {
  301. content = m.Content
  302. if !s.StateEnabled {
  303. content = m.ContentWithMentionsReplaced()
  304. return
  305. }
  306. channel, err := s.State.Channel(m.ChannelID)
  307. if err != nil {
  308. content = m.ContentWithMentionsReplaced()
  309. return
  310. }
  311. for _, user := range m.Mentions {
  312. nick := user.Username
  313. member, err := s.State.Member(channel.GuildID, user.ID)
  314. if err == nil && member.Nick != "" {
  315. nick = member.Nick
  316. }
  317. content = strings.NewReplacer(
  318. "<@"+user.ID+">", "@"+user.Username,
  319. "<@!"+user.ID+">", "@"+nick,
  320. ).Replace(content)
  321. }
  322. for _, roleID := range m.MentionRoles {
  323. role, err := s.State.Role(channel.GuildID, roleID)
  324. if err != nil || !role.Mentionable {
  325. continue
  326. }
  327. content = strings.Replace(content, "<@&"+role.ID+">", "@"+role.Name, -1)
  328. }
  329. content = patternChannels.ReplaceAllStringFunc(content, func(mention string) string {
  330. channel, err := s.State.Channel(mention[2 : len(mention)-1])
  331. if err != nil || channel.Type == ChannelTypeGuildVoice {
  332. return mention
  333. }
  334. return "#" + channel.Name
  335. })
  336. return
  337. }