app.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package ui
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "github.com/ayntgl/discordgo"
  7. "github.com/ayntgl/discordo/util"
  8. "github.com/gen2brain/beeep"
  9. "github.com/rivo/tview"
  10. )
  11. type App struct {
  12. *tview.Application
  13. MainFlex *tview.Flex
  14. GuildsList *tview.List
  15. ChannelsTreeView *tview.TreeView
  16. MessagesTextView *tview.TextView
  17. MessageInputField *tview.InputField
  18. Session *discordgo.Session
  19. SelectedChannel *discordgo.Channel
  20. Config util.Config
  21. SelectedMessage int
  22. }
  23. func NewApp() *App {
  24. s, _ := discordgo.New()
  25. return &App{
  26. Application: tview.NewApplication(),
  27. MainFlex: tview.NewFlex(),
  28. GuildsList: tview.NewList(),
  29. ChannelsTreeView: tview.NewTreeView(),
  30. MessagesTextView: tview.NewTextView(),
  31. MessageInputField: tview.NewInputField(),
  32. Session: s,
  33. Config: util.LoadConfig(),
  34. SelectedMessage: -1,
  35. }
  36. }
  37. func (app *App) Connect(token string) error {
  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. if app.Config.General.Notifications {
  82. for _, u := range m.Mentions {
  83. if u.ID == app.Session.State.User.ID {
  84. g, err := app.Session.State.Guild(m.GuildID)
  85. if err != nil {
  86. return
  87. }
  88. c, err := app.Session.State.Channel(m.ChannelID)
  89. if err != nil {
  90. return
  91. }
  92. go beeep.Alert(fmt.Sprintf("%s (#%s)", g.Name, c.Name), m.ContentWithMentionsReplaced(), "")
  93. }
  94. }
  95. }
  96. } else {
  97. app.SelectedChannel.Messages = append(app.SelectedChannel.Messages, m.Message)
  98. app.MessagesTextView.Write(buildMessage(app, m.Message))
  99. if len(app.MessagesTextView.GetHighlights()) == 0 {
  100. app.MessagesTextView.ScrollToEnd()
  101. }
  102. }
  103. }