message.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. "fmt"
  10. "regexp"
  11. )
  12. // A Message stores all data related to a specific Discord message.
  13. type Message struct {
  14. ID string `json:"id"`
  15. ChannelID string `json:"channel_id"`
  16. Content string `json:"content"`
  17. Timestamp Timestamp `json:"timestamp"`
  18. EditedTimestamp Timestamp `json:"edited_timestamp"`
  19. MentionRoles []string `json:"mention_roles"`
  20. Tts bool `json:"tts"`
  21. MentionEveryone bool `json:"mention_everyone"`
  22. Author *User `json:"author"`
  23. Attachments []*MessageAttachment `json:"attachments"`
  24. Embeds []*MessageEmbed `json:"embeds"`
  25. Mentions []*User `json:"mentions"`
  26. Reactions []*MessageReactions `json:"reactions"`
  27. }
  28. // MessageSend stores all parameters you can send with ChannelMessageSendComplex.
  29. type MessageSend struct {
  30. Content string `json:"content"`
  31. Tts bool `json:"tts"`
  32. Embed *MessageEmbed `json:"embed"`
  33. Nonce string `json:"nonce"`
  34. }
  35. // MessageEdit stores all parameters you can send with ChannelMessageSendComplex.
  36. type MessageEdit struct {
  37. Content string `json:"content"`
  38. Embed *MessageEmbed `json:"embed"`
  39. }
  40. // A MessageAttachment stores data for message attachments.
  41. type MessageAttachment struct {
  42. ID string `json:"id"`
  43. URL string `json:"url"`
  44. ProxyURL string `json:"proxy_url"`
  45. Filename string `json:"filename"`
  46. Width int `json:"width"`
  47. Height int `json:"height"`
  48. Size int `json:"size"`
  49. }
  50. // MessageEmbedFooter is a part of a MessageEmbed struct.
  51. type MessageEmbedFooter struct {
  52. Text string `json:"text,omitempty"`
  53. IconURL string `json:"icon_url,omitempty"`
  54. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  55. }
  56. // MessageEmbedImage is a part of a MessageEmbed struct.
  57. type MessageEmbedImage struct {
  58. URL string `json:"url,omitempty"`
  59. ProxyURL string `json:"proxy_url,omitempty"`
  60. Width int `json:"width,omitempty"`
  61. Height int `json:"height,omitempty"`
  62. }
  63. // MessageEmbedThumbnail is a part of a MessageEmbed struct.
  64. type MessageEmbedThumbnail struct {
  65. URL string `json:"url,omitempty"`
  66. ProxyURL string `json:"proxy_url,omitempty"`
  67. Width int `json:"width,omitempty"`
  68. Height int `json:"height,omitempty"`
  69. }
  70. // MessageEmbedVideo is a part of a MessageEmbed struct.
  71. type MessageEmbedVideo struct {
  72. URL string `json:"url,omitempty"`
  73. ProxyURL string `json:"proxy_url,omitempty"`
  74. Width int `json:"width,omitempty"`
  75. Height int `json:"height,omitempty"`
  76. }
  77. // MessageEmbedProvider is a part of a MessageEmbed struct.
  78. type MessageEmbedProvider struct {
  79. URL string `json:"url,omitempty"`
  80. Name string `json:"name,omitempty"`
  81. }
  82. // MessageEmbedAuthor is a part of a MessageEmbed struct.
  83. type MessageEmbedAuthor struct {
  84. URL string `json:"url,omitempty"`
  85. Name string `json:"name,omitempty"`
  86. IconURL string `json:"icon_url,omitempty"`
  87. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  88. }
  89. // MessageEmbedField is a part of a MessageEmbed struct.
  90. type MessageEmbedField struct {
  91. Name string `json:"name,omitempty"`
  92. Value string `json:"value,omitempty"`
  93. Inline bool `json:"inline,omitempty"`
  94. }
  95. // An MessageEmbed stores data for message embeds.
  96. type MessageEmbed struct {
  97. URL string `json:"url,omitempty"`
  98. Type string `json:"type,omitempty"`
  99. Title string `json:"title,omitempty"`
  100. Description string `json:"description,omitempty"`
  101. Timestamp string `json:"timestamp,omitempty"`
  102. Color int `json:"color,omitempty"`
  103. Footer *MessageEmbedFooter `json:"footer,omitempty"`
  104. Image *MessageEmbedImage `json:"image,omitempty"`
  105. Thumbnail *MessageEmbedThumbnail `json:"thumbnail,omitempty"`
  106. Video *MessageEmbedVideo `json:"video,omitempty"`
  107. Provider *MessageEmbedProvider `json:"provider,omitempty"`
  108. Author *MessageEmbedAuthor `json:"author,omitempty"`
  109. Fields []*MessageEmbedField `json:"fields,omitempty"`
  110. }
  111. // MessageReactions holds a reactions object for a message.
  112. type MessageReactions struct {
  113. Count int `json:"count"`
  114. Me bool `json:"me"`
  115. Emoji *Emoji `json:"emoji"`
  116. }
  117. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  118. // username of the mention.
  119. func (m *Message) ContentWithMentionsReplaced() string {
  120. if m.Mentions == nil {
  121. return m.Content
  122. }
  123. content := m.Content
  124. for _, user := range m.Mentions {
  125. content = regexp.MustCompile(fmt.Sprintf("<@!?(%s)>", user.ID)).ReplaceAllString(content, "@"+user.Username)
  126. }
  127. return content
  128. }