message.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // MessageParams is used in MessageSend and MessageEdit to share common parameters.
  29. type MessageParams struct {
  30. Content *string `json:"content,omitempty"`
  31. Embed *MessageEmbed `json:"embed,omitempty"`
  32. }
  33. // MessageSend stores all parameters you can send with ChannelMessageSendComplex.
  34. type MessageSend struct {
  35. MessageParams
  36. Tts bool `json:"tts"`
  37. }
  38. // MessageEdit stores all parameters you can send with ChannelMessageSendComplex.
  39. type MessageEdit struct {
  40. MessageParams
  41. }
  42. // SetContent is the same as setting the variable Content,
  43. // except it doesn't take a pointer.
  44. // Only a conveniance function.
  45. func (m *MessageParams) SetContent(str string) {
  46. m.Content = &str
  47. }
  48. // A MessageAttachment stores data for message attachments.
  49. type MessageAttachment struct {
  50. ID string `json:"id"`
  51. URL string `json:"url"`
  52. ProxyURL string `json:"proxy_url"`
  53. Filename string `json:"filename"`
  54. Width int `json:"width"`
  55. Height int `json:"height"`
  56. Size int `json:"size"`
  57. }
  58. // MessageEmbedFooter is a part of a MessageEmbed struct.
  59. type MessageEmbedFooter struct {
  60. Text string `json:"text,omitempty"`
  61. IconURL string `json:"icon_url,omitempty"`
  62. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  63. }
  64. // MessageEmbedImage is a part of a MessageEmbed struct.
  65. type MessageEmbedImage struct {
  66. URL string `json:"url,omitempty"`
  67. ProxyURL string `json:"proxy_url,omitempty"`
  68. Width int `json:"width,omitempty"`
  69. Height int `json:"height,omitempty"`
  70. }
  71. // MessageEmbedThumbnail is a part of a MessageEmbed struct.
  72. type MessageEmbedThumbnail struct {
  73. URL string `json:"url,omitempty"`
  74. ProxyURL string `json:"proxy_url,omitempty"`
  75. Width int `json:"width,omitempty"`
  76. Height int `json:"height,omitempty"`
  77. }
  78. // MessageEmbedVideo is a part of a MessageEmbed struct.
  79. type MessageEmbedVideo struct {
  80. URL string `json:"url,omitempty"`
  81. ProxyURL string `json:"proxy_url,omitempty"`
  82. Width int `json:"width,omitempty"`
  83. Height int `json:"height,omitempty"`
  84. }
  85. // MessageEmbedProvider is a part of a MessageEmbed struct.
  86. type MessageEmbedProvider struct {
  87. URL string `json:"url,omitempty"`
  88. Name string `json:"name,omitempty"`
  89. }
  90. // MessageEmbedAuthor is a part of a MessageEmbed struct.
  91. type MessageEmbedAuthor struct {
  92. URL string `json:"url,omitempty"`
  93. Name string `json:"name,omitempty"`
  94. IconURL string `json:"icon_url,omitempty"`
  95. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  96. }
  97. // MessageEmbedField is a part of a MessageEmbed struct.
  98. type MessageEmbedField struct {
  99. Name string `json:"name,omitempty"`
  100. Value string `json:"value,omitempty"`
  101. Inline bool `json:"inline,omitempty"`
  102. }
  103. // An MessageEmbed stores data for message embeds.
  104. type MessageEmbed struct {
  105. URL string `json:"url,omitempty"`
  106. Type string `json:"type,omitempty"`
  107. Title string `json:"title,omitempty"`
  108. Description string `json:"description,omitempty"`
  109. Timestamp string `json:"timestamp,omitempty"`
  110. Color int `json:"color,omitempty"`
  111. Footer *MessageEmbedFooter `json:"footer,omitempty"`
  112. Image *MessageEmbedImage `json:"image,omitempty"`
  113. Thumbnail *MessageEmbedThumbnail `json:"thumbnail,omitempty"`
  114. Video *MessageEmbedVideo `json:"video,omitempty"`
  115. Provider *MessageEmbedProvider `json:"provider,omitempty"`
  116. Author *MessageEmbedAuthor `json:"author,omitempty"`
  117. Fields []*MessageEmbedField `json:"fields,omitempty"`
  118. }
  119. // MessageReactions holds a reactions object for a message.
  120. type MessageReactions struct {
  121. Count int `json:"count"`
  122. Me bool `json:"me"`
  123. Emoji *Emoji `json:"emoji"`
  124. }
  125. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  126. // username of the mention.
  127. func (m *Message) ContentWithMentionsReplaced() string {
  128. if m.Mentions == nil {
  129. return m.Content
  130. }
  131. content := m.Content
  132. for _, user := range m.Mentions {
  133. content = regexp.MustCompile(fmt.Sprintf("<@!?(%s)>", user.ID)).ReplaceAllString(content, "@"+user.Username)
  134. }
  135. return content
  136. }