message.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. 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. // An MessageEmbed stores data for message embeds.
  38. type MessageEmbed struct {
  39. URL string `json:"url"`
  40. Type string `json:"type"`
  41. Title string `json:"title"`
  42. Description string `json:"description"`
  43. Thumbnail *struct {
  44. URL string `json:"url"`
  45. ProxyURL string `json:"proxy_url"`
  46. Width int `json:"width"`
  47. Height int `json:"height"`
  48. } `json:"thumbnail"`
  49. Provider *struct {
  50. URL string `json:"url"`
  51. Name string `json:"name"`
  52. } `json:"provider"`
  53. Author *struct {
  54. URL string `json:"url"`
  55. Name string `json:"name"`
  56. } `json:"author"`
  57. Video *struct {
  58. URL string `json:"url"`
  59. Width int `json:"width"`
  60. Height int `json:"height"`
  61. } `json:"video"`
  62. }
  63. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  64. // username of the mention.
  65. func (m *Message) ContentWithMentionsReplaced() string {
  66. if m.Mentions == nil {
  67. return m.Content
  68. }
  69. content := m.Content
  70. for _, user := range m.Mentions {
  71. content = strings.Replace(content, fmt.Sprintf("<@%s>", user.ID),
  72. fmt.Sprintf("@%s", user.Username), -1)
  73. content = strings.Replace(content, fmt.Sprintf("<@!%s>", user.ID),
  74. fmt.Sprintf("@%s", user.Username), -1)
  75. }
  76. return content
  77. }