app.go 2.5 KB

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