discord.go 1.5 KB

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