app.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package ui
  2. import (
  3. "sort"
  4. "strings"
  5. "github.com/ayntgl/discordgo"
  6. "github.com/ayntgl/discordo/util"
  7. "github.com/rivo/tview"
  8. )
  9. type App struct {
  10. *tview.Application
  11. MainFlex *tview.Flex
  12. GuildsList *tview.List
  13. ChannelsTreeView *tview.TreeView
  14. MessagesTextView *tview.TextView
  15. MessageInputField *tview.InputField
  16. Session *discordgo.Session
  17. SelectedChannel *discordgo.Channel
  18. Config *util.Config
  19. SelectedMessage int
  20. }
  21. func NewApp() *App {
  22. s, _ := discordgo.New()
  23. return &App{
  24. Application: tview.NewApplication(),
  25. MainFlex: tview.NewFlex(),
  26. GuildsList: tview.NewList(),
  27. ChannelsTreeView: tview.NewTreeView(),
  28. MessagesTextView: tview.NewTextView(),
  29. MessageInputField: tview.NewInputField(),
  30. Session: s,
  31. Config: util.LoadConfig(),
  32. SelectedMessage: -1,
  33. }
  34. }
  35. func (app *App) Connect(token string) error {
  36. // For user accounts, all of the guilds, the user is in, are dispatched in the READY gateway event.
  37. // Whereas, for bot accounts, the guilds are dispatched discretely in the GUILD_CREATE gateway events.
  38. if !strings.HasPrefix(token, "Bot") {
  39. app.Session.UserAgent = app.Config.General.UserAgent
  40. app.Session.Identify = discordgo.Identify{
  41. Compress: false,
  42. LargeThreshold: 0,
  43. Intents: 0,
  44. Properties: discordgo.IdentifyProperties{
  45. OS: "Linux",
  46. Browser: "Firefox",
  47. Device: "",
  48. },
  49. }
  50. app.Session.AddHandlerOnce(app.onSessionReady)
  51. }
  52. app.Session.Token = token
  53. app.Session.Identify.Token = token
  54. app.Session.AddHandler(app.onGuildCreate)
  55. app.Session.AddHandler(app.onSessionMessageCreate)
  56. return app.Session.Open()
  57. }
  58. func (app *App) onSessionReady(_ *discordgo.Session, r *discordgo.Ready) {
  59. app.GuildsList.AddItem("Direct Messages", "", 0, nil)
  60. sort.Slice(r.Guilds, func(a, b int) bool {
  61. found := false
  62. for _, guildID := range r.Settings.GuildPositions {
  63. if found && guildID == r.Guilds[b].ID {
  64. return true
  65. }
  66. if !found && guildID == r.Guilds[a].ID {
  67. found = true
  68. }
  69. }
  70. return false
  71. })
  72. for _, g := range r.Guilds {
  73. app.GuildsList.AddItem(g.Name, "", 0, nil)
  74. }
  75. }
  76. func (app *App) onGuildCreate(_ *discordgo.Session, g *discordgo.GuildCreate) {
  77. app.GuildsList.AddItem(g.Name, "", 0, nil)
  78. }
  79. func (app *App) onSessionMessageCreate(_ *discordgo.Session, m *discordgo.MessageCreate) {
  80. if app.SelectedChannel != nil && app.SelectedChannel.ID == m.ChannelID {
  81. app.SelectedChannel.Messages = append(app.SelectedChannel.Messages, m.Message)
  82. _, err := app.MessagesTextView.Write(buildMessage(app, m.Message))
  83. if err != nil {
  84. return
  85. }
  86. if len(app.MessagesTextView.GetHighlights()) == 0 {
  87. app.MessagesTextView.ScrollToEnd()
  88. }
  89. }
  90. }