message.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. // https://discord.com/developers/docs/resources/channel#message-object-message-types
  15. type MessageType int
  16. // Block contains the valid known MessageType values
  17. const (
  18. MessageTypeDefault MessageType = iota
  19. MessageTypeRecipientAdd
  20. MessageTypeRecipientRemove
  21. MessageTypeCall
  22. MessageTypeChannelNameChange
  23. MessageTypeChannelIconChange
  24. MessageTypeChannelPinnedMessage
  25. MessageTypeGuildMemberJoin
  26. MessageTypeUserPremiumGuildSubscription
  27. MessageTypeUserPremiumGuildSubscriptionTierOne
  28. MessageTypeUserPremiumGuildSubscriptionTierTwo
  29. MessageTypeUserPremiumGuildSubscriptionTierThree
  30. MessageTypeChannelFollowAdd
  31. MessageTypeGuildDiscoveryDisqualified = iota + 1
  32. MessageTypeGuildDiscoveryRequalified
  33. MessageTypeReply = iota + 4
  34. MessageTypeApplicationCommand
  35. )
  36. // A Message stores all data related to a specific Discord message.
  37. type Message struct {
  38. // The ID of the message.
  39. ID string `json:"id"`
  40. // The ID of the channel in which the message was sent.
  41. ChannelID string `json:"channel_id"`
  42. // The ID of the guild in which the message was sent.
  43. GuildID string `json:"guild_id,omitempty"`
  44. // The content of the message.
  45. Content string `json:"content"`
  46. // The time at which the messsage was sent.
  47. // CAUTION: this field may be removed in a
  48. // future API version; it is safer to calculate
  49. // the creation time via the ID.
  50. Timestamp Timestamp `json:"timestamp"`
  51. // The time at which the last edit of the message
  52. // occurred, if it has been edited.
  53. EditedTimestamp Timestamp `json:"edited_timestamp"`
  54. // The roles mentioned in the message.
  55. MentionRoles []string `json:"mention_roles"`
  56. // Whether the message is text-to-speech.
  57. TTS bool `json:"tts"`
  58. // Whether the message mentions everyone.
  59. MentionEveryone bool `json:"mention_everyone"`
  60. // The author of the message. This is not guaranteed to be a
  61. // valid user (webhook-sent messages do not possess a full author).
  62. Author *User `json:"author"`
  63. // A list of attachments present in the message.
  64. Attachments []*MessageAttachment `json:"attachments"`
  65. // A list of embeds present in the message. Multiple
  66. // embeds can currently only be sent by webhooks.
  67. Embeds []*MessageEmbed `json:"embeds"`
  68. // A list of users mentioned in the message.
  69. Mentions []*User `json:"mentions"`
  70. // A list of reactions to the message.
  71. Reactions []*MessageReactions `json:"reactions"`
  72. // Whether the message is pinned or not.
  73. Pinned bool `json:"pinned"`
  74. // The type of the message.
  75. Type MessageType `json:"type"`
  76. // The webhook ID of the message, if it was generated by a webhook
  77. WebhookID string `json:"webhook_id"`
  78. // Member properties for this message's author,
  79. // contains only partial information
  80. Member *Member `json:"member"`
  81. // Channels specifically mentioned in this message
  82. // Not all channel mentions in a message will appear in mention_channels.
  83. // Only textual channels that are visible to everyone in a lurkable guild will ever be included.
  84. // Only crossposted messages (via Channel Following) currently include mention_channels at all.
  85. // If no mentions in the message meet these requirements, this field will not be sent.
  86. MentionChannels []*Channel `json:"mention_channels"`
  87. // Is sent with Rich Presence-related chat embeds
  88. Activity *MessageActivity `json:"activity"`
  89. // Is sent with Rich Presence-related chat embeds
  90. Application *MessageApplication `json:"application"`
  91. // MessageReference contains reference data sent with crossposted messages
  92. MessageReference *MessageReference `json:"message_reference"`
  93. // The flags of the message, which describe extra features of a message.
  94. // This is a combination of bit masks; the presence of a certain permission can
  95. // be checked by performing a bitwise AND between this int and the flag.
  96. Flags MessageFlags `json:"flags"`
  97. }
  98. // MessageFlags is the flags of "message" (see MessageFlags* consts)
  99. // https://discord.com/developers/docs/resources/channel#message-object-message-flags
  100. type MessageFlags int
  101. // Valid MessageFlags values
  102. const (
  103. MessageFlagsCrossPosted MessageFlags = 1 << iota
  104. MessageFlagsIsCrossPosted
  105. MessageFlagsSupressEmbeds
  106. MessageFlagsSourceMessageDeleted
  107. MessageFlagsUrgent
  108. )
  109. // File stores info about files you e.g. send in messages.
  110. type File struct {
  111. Name string
  112. ContentType string
  113. Reader io.Reader
  114. }
  115. // MessageSend stores all parameters you can send with ChannelMessageSendComplex.
  116. type MessageSend struct {
  117. Content string `json:"content,omitempty"`
  118. Embed *MessageEmbed `json:"embed,omitempty"`
  119. TTS bool `json:"tts"`
  120. Files []*File `json:"-"`
  121. AllowedMentions *MessageAllowedMentions `json:"allowed_mentions,omitempty"`
  122. Reference *MessageReference `json:"message_reference,omitempty"`
  123. // TODO: Remove this when compatibility is not required.
  124. File *File `json:"-"`
  125. }
  126. // MessageEdit is used to chain parameters via ChannelMessageEditComplex, which
  127. // is also where you should get the instance from.
  128. type MessageEdit struct {
  129. Content *string `json:"content,omitempty"`
  130. Embed *MessageEmbed `json:"embed,omitempty"`
  131. AllowedMentions *MessageAllowedMentions `json:"allowed_mentions,omitempty"`
  132. ID string
  133. Channel string
  134. }
  135. // NewMessageEdit returns a MessageEdit struct, initialized
  136. // with the Channel and ID.
  137. func NewMessageEdit(channelID string, messageID string) *MessageEdit {
  138. return &MessageEdit{
  139. Channel: channelID,
  140. ID: messageID,
  141. }
  142. }
  143. // SetContent is the same as setting the variable Content,
  144. // except it doesn't take a pointer.
  145. func (m *MessageEdit) SetContent(str string) *MessageEdit {
  146. m.Content = &str
  147. return m
  148. }
  149. // SetEmbed is a convenience function for setting the embed,
  150. // so you can chain commands.
  151. func (m *MessageEdit) SetEmbed(embed *MessageEmbed) *MessageEdit {
  152. m.Embed = embed
  153. return m
  154. }
  155. // AllowedMentionType describes the types of mentions used
  156. // in the MessageAllowedMentions type.
  157. type AllowedMentionType string
  158. // The types of mentions used in MessageAllowedMentions.
  159. const (
  160. AllowedMentionTypeRoles AllowedMentionType = "roles"
  161. AllowedMentionTypeUsers AllowedMentionType = "users"
  162. AllowedMentionTypeEveryone AllowedMentionType = "everyone"
  163. )
  164. // MessageAllowedMentions allows the user to specify which mentions
  165. // Discord is allowed to parse in this message. This is useful when
  166. // sending user input as a message, as it prevents unwanted mentions.
  167. // If this type is used, all mentions must be explicitly whitelisted,
  168. // either by putting an AllowedMentionType in the Parse slice
  169. // (allowing all mentions of that type) or, in the case of roles and
  170. // users, explicitly allowing those mentions on an ID-by-ID basis.
  171. // For more information on this functionality, see:
  172. // https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object-allowed-mentions-reference
  173. type MessageAllowedMentions struct {
  174. // The mention types that are allowed to be parsed in this message.
  175. // Please note that this is purposely **not** marked as omitempty,
  176. // so if a zero-value MessageAllowedMentions object is provided no
  177. // mentions will be allowed.
  178. Parse []AllowedMentionType `json:"parse"`
  179. // A list of role IDs to allow. This cannot be used when specifying
  180. // AllowedMentionTypeRoles in the Parse slice.
  181. Roles []string `json:"roles,omitempty"`
  182. // A list of user IDs to allow. This cannot be used when specifying
  183. // AllowedMentionTypeUsers in the Parse slice.
  184. Users []string `json:"users,omitempty"`
  185. }
  186. // A MessageAttachment stores data for message attachments.
  187. type MessageAttachment struct {
  188. ID string `json:"id"`
  189. URL string `json:"url"`
  190. ProxyURL string `json:"proxy_url"`
  191. Filename string `json:"filename"`
  192. Width int `json:"width"`
  193. Height int `json:"height"`
  194. Size int `json:"size"`
  195. }
  196. // MessageEmbedFooter is a part of a MessageEmbed struct.
  197. type MessageEmbedFooter struct {
  198. Text string `json:"text,omitempty"`
  199. IconURL string `json:"icon_url,omitempty"`
  200. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  201. }
  202. // MessageEmbedImage is a part of a MessageEmbed struct.
  203. type MessageEmbedImage struct {
  204. URL string `json:"url,omitempty"`
  205. ProxyURL string `json:"proxy_url,omitempty"`
  206. Width int `json:"width,omitempty"`
  207. Height int `json:"height,omitempty"`
  208. }
  209. // MessageEmbedThumbnail is a part of a MessageEmbed struct.
  210. type MessageEmbedThumbnail struct {
  211. URL string `json:"url,omitempty"`
  212. ProxyURL string `json:"proxy_url,omitempty"`
  213. Width int `json:"width,omitempty"`
  214. Height int `json:"height,omitempty"`
  215. }
  216. // MessageEmbedVideo is a part of a MessageEmbed struct.
  217. type MessageEmbedVideo struct {
  218. URL string `json:"url,omitempty"`
  219. Width int `json:"width,omitempty"`
  220. Height int `json:"height,omitempty"`
  221. }
  222. // MessageEmbedProvider is a part of a MessageEmbed struct.
  223. type MessageEmbedProvider struct {
  224. URL string `json:"url,omitempty"`
  225. Name string `json:"name,omitempty"`
  226. }
  227. // MessageEmbedAuthor is a part of a MessageEmbed struct.
  228. type MessageEmbedAuthor struct {
  229. URL string `json:"url,omitempty"`
  230. Name string `json:"name,omitempty"`
  231. IconURL string `json:"icon_url,omitempty"`
  232. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  233. }
  234. // MessageEmbedField is a part of a MessageEmbed struct.
  235. type MessageEmbedField struct {
  236. Name string `json:"name,omitempty"`
  237. Value string `json:"value,omitempty"`
  238. Inline bool `json:"inline,omitempty"`
  239. }
  240. // An MessageEmbed stores data for message embeds.
  241. type MessageEmbed struct {
  242. URL string `json:"url,omitempty"`
  243. Type EmbedType `json:"type,omitempty"`
  244. Title string `json:"title,omitempty"`
  245. Description string `json:"description,omitempty"`
  246. Timestamp string `json:"timestamp,omitempty"`
  247. Color int `json:"color,omitempty"`
  248. Footer *MessageEmbedFooter `json:"footer,omitempty"`
  249. Image *MessageEmbedImage `json:"image,omitempty"`
  250. Thumbnail *MessageEmbedThumbnail `json:"thumbnail,omitempty"`
  251. Video *MessageEmbedVideo `json:"video,omitempty"`
  252. Provider *MessageEmbedProvider `json:"provider,omitempty"`
  253. Author *MessageEmbedAuthor `json:"author,omitempty"`
  254. Fields []*MessageEmbedField `json:"fields,omitempty"`
  255. }
  256. // EmbedType is the type of embed
  257. // https://discord.com/developers/docs/resources/channel#embed-object-embed-types
  258. type EmbedType string
  259. // Block of valid EmbedTypes
  260. const (
  261. EmbedTypeRich EmbedType = "rich"
  262. EmbedTypeImage EmbedType = "image"
  263. EmbedTypeVideo EmbedType = "video"
  264. EmbedTypeGifv EmbedType = "gifv"
  265. EmbedTypeArticle EmbedType = "article"
  266. EmbedTypeLink EmbedType = "link"
  267. )
  268. // MessageReactions holds a reactions object for a message.
  269. type MessageReactions struct {
  270. Count int `json:"count"`
  271. Me bool `json:"me"`
  272. Emoji *Emoji `json:"emoji"`
  273. }
  274. // MessageActivity is sent with Rich Presence-related chat embeds
  275. type MessageActivity struct {
  276. Type MessageActivityType `json:"type"`
  277. PartyID string `json:"party_id"`
  278. }
  279. // MessageActivityType is the type of message activity
  280. type MessageActivityType int
  281. // Constants for the different types of Message Activity
  282. const (
  283. MessageActivityTypeJoin MessageActivityType = iota + 1
  284. MessageActivityTypeSpectate
  285. MessageActivityTypeListen
  286. MessageActivityTypeJoinRequest
  287. )
  288. // MessageFlag describes an extra feature of the message
  289. type MessageFlag int
  290. // Constants for the different bit offsets of Message Flags
  291. const (
  292. // This message has been published to subscribed channels (via Channel Following)
  293. MessageFlagCrossposted MessageFlag = 1 << iota
  294. // This message originated from a message in another channel (via Channel Following)
  295. MessageFlagIsCrosspost
  296. // Do not include any embeds when serializing this message
  297. MessageFlagSuppressEmbeds
  298. )
  299. // MessageApplication is sent with Rich Presence-related chat embeds
  300. type MessageApplication struct {
  301. ID string `json:"id"`
  302. CoverImage string `json:"cover_image"`
  303. Description string `json:"description"`
  304. Icon string `json:"icon"`
  305. Name string `json:"name"`
  306. }
  307. // MessageReference contains reference data sent with crossposted messages
  308. type MessageReference struct {
  309. MessageID string `json:"message_id"`
  310. ChannelID string `json:"channel_id"`
  311. GuildID string `json:"guild_id,omitempty"`
  312. }
  313. // Reference returns MessageReference of given message
  314. func (m *Message) Reference() *MessageReference {
  315. return &MessageReference{
  316. GuildID: m.GuildID,
  317. ChannelID: m.ChannelID,
  318. MessageID: m.ID,
  319. }
  320. }
  321. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  322. // username of the mention.
  323. func (m *Message) ContentWithMentionsReplaced() (content string) {
  324. content = m.Content
  325. for _, user := range m.Mentions {
  326. content = strings.NewReplacer(
  327. "<@"+user.ID+">", "@"+user.Username,
  328. "<@!"+user.ID+">", "@"+user.Username,
  329. ).Replace(content)
  330. }
  331. return
  332. }
  333. var patternChannels = regexp.MustCompile("<#[^>]*>")
  334. // ContentWithMoreMentionsReplaced will replace all @<id> mentions with the
  335. // username of the mention, but also role IDs and more.
  336. func (m *Message) ContentWithMoreMentionsReplaced(s *Session) (content string, err error) {
  337. content = m.Content
  338. if !s.StateEnabled {
  339. content = m.ContentWithMentionsReplaced()
  340. return
  341. }
  342. channel, err := s.State.Channel(m.ChannelID)
  343. if err != nil {
  344. content = m.ContentWithMentionsReplaced()
  345. return
  346. }
  347. for _, user := range m.Mentions {
  348. nick := user.Username
  349. member, err := s.State.Member(channel.GuildID, user.ID)
  350. if err == nil && member.Nick != "" {
  351. nick = member.Nick
  352. }
  353. content = strings.NewReplacer(
  354. "<@"+user.ID+">", "@"+user.Username,
  355. "<@!"+user.ID+">", "@"+nick,
  356. ).Replace(content)
  357. }
  358. for _, roleID := range m.MentionRoles {
  359. role, err := s.State.Role(channel.GuildID, roleID)
  360. if err != nil || !role.Mentionable {
  361. continue
  362. }
  363. content = strings.Replace(content, "<@&"+role.ID+">", "@"+role.Name, -1)
  364. }
  365. content = patternChannels.ReplaceAllStringFunc(content, func(mention string) string {
  366. channel, err := s.State.Channel(mention[2 : len(mention)-1])
  367. if err != nil || channel.Type == ChannelTypeGuildVoice {
  368. return mention
  369. }
  370. return "#" + channel.Name
  371. })
  372. return
  373. }