message.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "io"
  11. "regexp"
  12. )
  13. // A Message stores all data related to a specific Discord message.
  14. type Message struct {
  15. ID string `json:"id"`
  16. ChannelID string `json:"channel_id"`
  17. Content string `json:"content"`
  18. Timestamp Timestamp `json:"timestamp"`
  19. EditedTimestamp Timestamp `json:"edited_timestamp"`
  20. MentionRoles []string `json:"mention_roles"`
  21. Tts bool `json:"tts"`
  22. MentionEveryone bool `json:"mention_everyone"`
  23. Author *User `json:"author"`
  24. Attachments []*MessageAttachment `json:"attachments"`
  25. Embeds []*MessageEmbed `json:"embeds"`
  26. Mentions []*User `json:"mentions"`
  27. Reactions []*MessageReactions `json:"reactions"`
  28. }
  29. // File stores info about files you e.g. send in messages.
  30. type File struct {
  31. Name string
  32. Reader io.Reader
  33. }
  34. // MessageSend stores all parameters you can send with ChannelMessageSendComplex.
  35. type MessageSend struct {
  36. Content string `json:"content,omitempty"`
  37. Embed *MessageEmbed `json:"embed,omitempty"`
  38. Tts bool `json:"tts"`
  39. File *File `json:"file"`
  40. }
  41. // MessageEdit is used to chain parameters via ChannelMessageEditComplex, which
  42. // is also where you should get the instance from.
  43. type MessageEdit struct {
  44. Content *string `json:"content,omitempty"`
  45. Embed *MessageEmbed `json:"embed,omitempty"`
  46. ID string
  47. Channel string
  48. }
  49. // NewMessageEdit returns a MessageEdit struct, initialized
  50. // with the Channel and ID.
  51. func NewMessageEdit(channelID string, messageID string) *MessageEdit {
  52. return &MessageEdit{
  53. Channel: channelID,
  54. ID: messageID,
  55. }
  56. }
  57. // SetContent is the same as setting the variable Content,
  58. // except it doesn't take a pointer.
  59. func (m *MessageEdit) SetContent(str string) *MessageEdit {
  60. m.Content = &str
  61. return m
  62. }
  63. // SetEmbed is a convenience function for setting the embed,
  64. // so you can chain commands.
  65. func (m *MessageEdit) SetEmbed(embed *MessageEmbed) *MessageEdit {
  66. m.Embed = embed
  67. return m
  68. }
  69. // A MessageAttachment stores data for message attachments.
  70. type MessageAttachment struct {
  71. ID string `json:"id"`
  72. URL string `json:"url"`
  73. ProxyURL string `json:"proxy_url"`
  74. Filename string `json:"filename"`
  75. Width int `json:"width"`
  76. Height int `json:"height"`
  77. Size int `json:"size"`
  78. }
  79. // MessageEmbedFooter is a part of a MessageEmbed struct.
  80. type MessageEmbedFooter struct {
  81. Text string `json:"text,omitempty"`
  82. IconURL string `json:"icon_url,omitempty"`
  83. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  84. }
  85. // MessageEmbedImage is a part of a MessageEmbed struct.
  86. type MessageEmbedImage 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. // MessageEmbedThumbnail is a part of a MessageEmbed struct.
  93. type MessageEmbedThumbnail 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. // MessageEmbedVideo is a part of a MessageEmbed struct.
  100. type MessageEmbedVideo struct {
  101. URL string `json:"url,omitempty"`
  102. ProxyURL string `json:"proxy_url,omitempty"`
  103. Width int `json:"width,omitempty"`
  104. Height int `json:"height,omitempty"`
  105. }
  106. // MessageEmbedProvider is a part of a MessageEmbed struct.
  107. type MessageEmbedProvider struct {
  108. URL string `json:"url,omitempty"`
  109. Name string `json:"name,omitempty"`
  110. }
  111. // MessageEmbedAuthor is a part of a MessageEmbed struct.
  112. type MessageEmbedAuthor struct {
  113. URL string `json:"url,omitempty"`
  114. Name string `json:"name,omitempty"`
  115. IconURL string `json:"icon_url,omitempty"`
  116. ProxyIconURL string `json:"proxy_icon_url,omitempty"`
  117. }
  118. // MessageEmbedField is a part of a MessageEmbed struct.
  119. type MessageEmbedField struct {
  120. Name string `json:"name,omitempty"`
  121. Value string `json:"value,omitempty"`
  122. Inline bool `json:"inline,omitempty"`
  123. }
  124. // An MessageEmbed stores data for message embeds.
  125. type MessageEmbed struct {
  126. URL string `json:"url,omitempty"`
  127. Type string `json:"type,omitempty"`
  128. Title string `json:"title,omitempty"`
  129. Description string `json:"description,omitempty"`
  130. Timestamp string `json:"timestamp,omitempty"`
  131. Color int `json:"color,omitempty"`
  132. Footer *MessageEmbedFooter `json:"footer,omitempty"`
  133. Image *MessageEmbedImage `json:"image,omitempty"`
  134. Thumbnail *MessageEmbedThumbnail `json:"thumbnail,omitempty"`
  135. Video *MessageEmbedVideo `json:"video,omitempty"`
  136. Provider *MessageEmbedProvider `json:"provider,omitempty"`
  137. Author *MessageEmbedAuthor `json:"author,omitempty"`
  138. Fields []*MessageEmbedField `json:"fields,omitempty"`
  139. }
  140. // MessageReactions holds a reactions object for a message.
  141. type MessageReactions struct {
  142. Count int `json:"count"`
  143. Me bool `json:"me"`
  144. Emoji *Emoji `json:"emoji"`
  145. }
  146. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  147. // username of the mention.
  148. func (m *Message) ContentWithMentionsReplaced() string {
  149. if m.Mentions == nil {
  150. return m.Content
  151. }
  152. content := m.Content
  153. for _, user := range m.Mentions {
  154. content = regexp.MustCompile(fmt.Sprintf("<@!?(%s)>", user.ID)).ReplaceAllString(content, "@"+user.Username)
  155. }
  156. return content
  157. }