message.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. "strings"
  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 string `json:"timestamp"`
  18. EditedTimestamp string `json:"edited_timestamp"`
  19. Tts bool `json:"tts"`
  20. MentionEveryone bool `json:"mention_everyone"`
  21. Author *User `json:"author"`
  22. Attachments []*Attachment `json:"attachments"`
  23. Embeds []*Embed `json:"embeds"`
  24. Mentions []*User `json:"mentions"`
  25. }
  26. // An Attachment stores data for message attachments.
  27. type Attachment struct {
  28. ID string `json:"id"`
  29. URL string `json:"url"`
  30. ProxyURL string `json:"proxy_url"`
  31. Width int `json:"width"`
  32. Height int `json:"height"`
  33. Filename string `json:"filename"`
  34. Size int `json:"size"`
  35. }
  36. // An Embed stores data for message embeds.
  37. type Embed struct {
  38. URL string `json:"url"`
  39. Type string `json:"type"`
  40. Title string `json:"title"`
  41. Description string `json:"description"`
  42. Thumbnail *struct {
  43. URL string `json:"url"`
  44. ProxyURL string `json:"proxy_url"`
  45. Width int `json:"width"`
  46. Height int `json:"height"`
  47. } `json:"thumbnail"`
  48. Provider *struct {
  49. URL string `json:"url"`
  50. Name string `json:"name"`
  51. } `json:"provider"`
  52. Author *struct {
  53. URL string `json:"url"`
  54. Name string `json:"name"`
  55. } `json:"author"`
  56. Video *struct {
  57. URL string `json:"url"`
  58. Width int `json:"width"`
  59. Height int `json:"height"`
  60. } `json:"video"`
  61. }
  62. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  63. // username of the mention.
  64. func (m *Message) ContentWithMentionsReplaced() string {
  65. if m.Mentions == nil {
  66. return m.Content
  67. }
  68. content := m.Content
  69. for _, user := range m.Mentions {
  70. content = strings.Replace(content, fmt.Sprintf("<@%s>", user.ID),
  71. fmt.Sprintf("@%s", user.Username), -1)
  72. }
  73. return content
  74. }