app.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package ui
  2. import (
  3. "fmt"
  4. "sort"
  5. "github.com/ayntgl/discordgo"
  6. "github.com/ayntgl/discordo/config"
  7. "github.com/gen2brain/beeep"
  8. "github.com/rivo/tview"
  9. )
  10. type App struct {
  11. *tview.Application
  12. LoginForm *tview.Form
  13. GuildsList *tview.List
  14. ChannelsTreeView *tview.TreeView
  15. MessagesTextView *tview.TextView
  16. MessageInputField *tview.InputField
  17. Session *discordgo.Session
  18. SelectedChannel *discordgo.Channel
  19. SelectedMessage int
  20. Config config.Config
  21. }
  22. func NewApp() *App {
  23. s, _ := discordgo.New()
  24. return &App{
  25. Application: tview.NewApplication(),
  26. LoginForm: tview.NewForm(),
  27. GuildsList: tview.NewList(),
  28. ChannelsTreeView: tview.NewTreeView(),
  29. MessagesTextView: tview.NewTextView(),
  30. MessageInputField: tview.NewInputField(),
  31. Session: s,
  32. SelectedMessage: -1,
  33. }
  34. }
  35. func (app *App) Connect(token string) error {
  36. app.Session.Token = token
  37. app.Session.UserAgent = app.Config.General.UserAgent
  38. app.Session.Identify = discordgo.Identify{
  39. Token: token,
  40. Compress: false,
  41. LargeThreshold: 0,
  42. Intents: 0,
  43. Properties: discordgo.IdentifyProperties{
  44. OS: "Linux",
  45. Browser: "Firefox",
  46. Device: "",
  47. },
  48. }
  49. app.Session.AddHandlerOnce(app.onSessionReady)
  50. app.Session.AddHandler(app.onSessionMessageCreate)
  51. return app.Session.Open()
  52. }
  53. func (app *App) onSessionReady(_ *discordgo.Session, r *discordgo.Ready) {
  54. sort.Slice(r.Guilds, func(a, b int) bool {
  55. found := false
  56. for _, guildID := range r.Settings.GuildPositions {
  57. if found && guildID == r.Guilds[b].ID {
  58. return true
  59. }
  60. if !found && guildID == r.Guilds[a].ID {
  61. found = true
  62. }
  63. }
  64. return false
  65. })
  66. for _, g := range r.Guilds {
  67. app.GuildsList.AddItem(g.Name, "", 0, nil)
  68. }
  69. }
  70. func (app *App) onSessionMessageCreate(_ *discordgo.Session, m *discordgo.MessageCreate) {
  71. if app.SelectedChannel == nil || app.SelectedChannel.ID != m.ChannelID {
  72. if app.Config.General.Notifications {
  73. for _, u := range m.Mentions {
  74. if u.ID == app.Session.State.User.ID {
  75. g, err := app.Session.State.Guild(m.GuildID)
  76. if err != nil {
  77. return
  78. }
  79. c, err := app.Session.State.Channel(m.ChannelID)
  80. if err != nil {
  81. return
  82. }
  83. go beeep.Alert(fmt.Sprintf("%s (#%s)", g.Name, c.Name), m.ContentWithMentionsReplaced(), "")
  84. }
  85. }
  86. }
  87. } else {
  88. app.SelectedChannel.Messages = append(app.SelectedChannel.Messages, m.Message)
  89. app.MessagesTextView.Write(buildMessage(app, m.Message))
  90. if len(app.MessagesTextView.GetHighlights()) == 0 {
  91. app.MessagesTextView.ScrollToEnd()
  92. }
  93. }
  94. }