app.go 2.6 KB

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