discord.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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("[red::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("[blue]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. attachmentsLen := len(attachments)
  27. if attachmentsLen > 0 {
  28. for i := 0; i < attachmentsLen; i++ {
  29. content.WriteString("\n" + attachments[i].URL)
  30. }
  31. }
  32. fmt.Fprintln(messagesTextView, content.String())
  33. }
  34. func SendMessage(session *session.Session, channelID discord.ChannelID, content string) {
  35. _, err := session.SendText(channelID, content)
  36. if err != nil {
  37. panic(err)
  38. }
  39. }
  40. func GetMessages(session *session.Session, channelID discord.ChannelID, limit uint) (messages []discord.Message) {
  41. messages, err := session.Messages(channelID, limit)
  42. if err != nil {
  43. panic(err)
  44. }
  45. return
  46. }