message.go 5.7 KB

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