app.go 2.5 KB

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