message.go 4.6 KB

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