message.go 4.4 KB

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