message.go 5.9 KB

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