message.go 14 KB

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