discord.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package util
  2. import (
  3. "fmt"
  4. _ "image/jpeg"
  5. _ "image/png"
  6. "strings"
  7. "github.com/diamondburned/arikawa/v2/discord"
  8. "github.com/diamondburned/arikawa/v2/session"
  9. "github.com/rivo/tview"
  10. )
  11. func WriteMessage(messagesTextView *tview.TextView, message discord.Message) {
  12. var content strings.Builder
  13. content.WriteString("[#E95678::b]" + message.Author.Username + "[-:-:-] ")
  14. // If the author of the message is a bot account, add "BOT" beside the username of the author.
  15. if message.Author.Bot {
  16. content.WriteString("[#26BBD9]BOT[-:-:-] ")
  17. }
  18. if message.Content != "" {
  19. content.WriteString(message.Content)
  20. }
  21. // TODO(rigormorrtiss): display the message embed using "special" format
  22. if len(message.Embeds) > 0 {
  23. content.WriteString("\n<EMBED>")
  24. }
  25. attachments := message.Attachments
  26. for i := range attachments {
  27. content.WriteString("\n" + attachments[i].URL)
  28. }
  29. fmt.Fprintln(messagesTextView, content.String())
  30. }
  31. func SendMessage(session *session.Session, channelID discord.ChannelID, content string) {
  32. _, err := session.SendText(channelID, content)
  33. if err != nil {
  34. panic(err)
  35. }
  36. }
  37. func GetMessages(session *session.Session, channelID discord.ChannelID, limit uint) (messages []discord.Message) {
  38. messages, err := session.Messages(channelID, limit)
  39. if err != nil {
  40. panic(err)
  41. }
  42. return
  43. }