message.go 14 KB

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