discordo.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package main
  2. import (
  3. "strings"
  4. "github.com/bwmarrin/discordgo"
  5. "github.com/gdamore/tcell/v2"
  6. "github.com/rigormorrtiss/discordo/ui"
  7. "github.com/rigormorrtiss/discordo/util"
  8. "github.com/rivo/tview"
  9. )
  10. var (
  11. app *tview.Application
  12. loginModal *tview.Modal
  13. loginForm *tview.Form
  14. guildsDropDown *tview.DropDown
  15. channelsList *tview.List
  16. messagesTextView *tview.TextView
  17. messageInputField *tview.InputField
  18. mainFlex *tview.Flex
  19. loginVia string
  20. theme *util.Theme
  21. session *discordgo.Session
  22. currentGuild *discordgo.Guild
  23. currentChannel *discordgo.Channel
  24. )
  25. func main() {
  26. tview.Borders.HorizontalFocus = tview.Borders.Horizontal
  27. tview.Borders.VerticalFocus = tview.Borders.Vertical
  28. tview.Borders.TopLeftFocus = tview.Borders.TopLeft
  29. tview.Borders.TopRightFocus = tview.Borders.TopRight
  30. tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
  31. tview.Borders.BottomRightFocus = tview.Borders.BottomRight
  32. tview.Borders.Horizontal = ' '
  33. tview.Borders.Vertical = ' '
  34. tview.Borders.TopLeft = ' '
  35. tview.Borders.TopRight = ' '
  36. tview.Borders.BottomLeft = ' '
  37. tview.Borders.BottomRight = ' '
  38. theme = util.NewTheme()
  39. loginModal = ui.NewLoginModal(onLoginModalDone)
  40. guildsDropDown = ui.NewGuildsDropDown(onGuildsDropDownSelected, theme)
  41. channelsList = ui.NewChannelsList(onChannelsListSelected, theme)
  42. messagesTextView = ui.NewMessagesTextView(onMessagesTextViewChanged, theme)
  43. mainFlex = ui.NewMainFlex(guildsDropDown, channelsList, messagesTextView)
  44. app = ui.NewApp()
  45. token := util.GetPassword("token")
  46. if token != "" {
  47. app.
  48. SetRoot(mainFlex, true).
  49. SetFocus(guildsDropDown)
  50. session = newSession("", "", token)
  51. } else {
  52. app.SetRoot(loginModal, true)
  53. }
  54. if err := app.Run(); err != nil {
  55. panic(err)
  56. }
  57. }
  58. func onLoginFormQuitButtonSelected() {
  59. app.Stop()
  60. }
  61. func onMessagesTextViewChanged() {
  62. app.Draw()
  63. }
  64. func onLoginModalDone(buttonIndex int, buttonLabel string) {
  65. if buttonLabel == ui.LoginViaEmailPasswordLoginModalButton {
  66. loginVia = "emailpassword"
  67. loginForm = ui.NewLoginForm(loginVia, onLoginFormLoginButtonSelected, onLoginFormQuitButtonSelected)
  68. app.SetRoot(loginForm, true)
  69. } else if buttonLabel == ui.LoginViaTokenLoginModalButton {
  70. loginVia = "token"
  71. loginForm = ui.NewLoginForm(loginVia, onLoginFormLoginButtonSelected, onLoginFormQuitButtonSelected)
  72. app.SetRoot(loginForm, true)
  73. }
  74. }
  75. func newSession(email string, password string, token string) *discordgo.Session {
  76. userAgent := "" +
  77. "Mozilla/5.0 (X11; Linux x86_64) " +
  78. "AppleWebKit/537.36 (KHTML, like Gecko) " +
  79. "Chrome/91.0.4472.164 Safari/537.36"
  80. var sess *discordgo.Session
  81. var err error
  82. if email != "" && password != "" {
  83. sess, err = discordgo.New(email, password)
  84. if err != nil {
  85. panic(err)
  86. }
  87. sess.UserAgent = userAgent
  88. sess.Identify.Properties.Browser = "Chrome"
  89. sess.Identify.Properties.OS = "Linux"
  90. sess.AddHandler(onReady)
  91. } else if token != "" {
  92. sess, err = discordgo.New(token)
  93. if err != nil {
  94. panic(err)
  95. }
  96. if !strings.HasPrefix(token, "Bot ") {
  97. sess.UserAgent = userAgent
  98. sess.Identify.Properties.Browser = "Chrome"
  99. sess.Identify.Properties.OS = "Linux"
  100. sess.AddHandler(onReady)
  101. }
  102. }
  103. sess.AddHandler(onGuildCreate)
  104. sess.AddHandler(onMessageCreate)
  105. if err = sess.Open(); err != nil {
  106. panic(err)
  107. }
  108. return sess
  109. }
  110. func onGuildCreate(_ *discordgo.Session, guild *discordgo.GuildCreate) {
  111. guildsDropDown.AddOption(guild.Name, nil)
  112. }
  113. func onReady(_ *discordgo.Session, ready *discordgo.Ready) {
  114. for i := range ready.Guilds {
  115. guildsDropDown.AddOption(ready.Guilds[i].Name, nil)
  116. }
  117. }
  118. func onMessageCreate(_ *discordgo.Session, message *discordgo.MessageCreate) {
  119. if currentChannel != nil && currentChannel.ID == message.ChannelID {
  120. util.WriteMessage(messagesTextView, session, message.Message)
  121. }
  122. }
  123. func onGuildsDropDownSelected(text string, _ int) {
  124. channelsList.Clear()
  125. messagesTextView.Clear()
  126. if messageInputField != nil {
  127. mainFlex.RemoveItem(messageInputField)
  128. messageInputField = nil
  129. }
  130. guilds := session.State.Guilds
  131. for i := range guilds {
  132. guild := guilds[i]
  133. if guild.Name == text {
  134. currentGuild = guild
  135. break
  136. }
  137. }
  138. channelsList.SetTitle("Channels")
  139. app.SetFocus(channelsList)
  140. for i := range currentGuild.Channels {
  141. channel := currentGuild.Channels[i]
  142. channelsList.AddItem(channel.Name, "", 0, nil)
  143. }
  144. }
  145. func onChannelsListSelected(i int, mainText string, secondaryText string, _ rune) {
  146. messagesTextView.Clear()
  147. if messageInputField == nil {
  148. messageInputField = ui.NewMessageInputField(onMessageInputFieldDone, theme)
  149. mainFlex.AddItem(messageInputField, 3, 1, false)
  150. }
  151. currentChannel = currentGuild.Channels[i]
  152. messagesTextView.SetTitle(currentChannel.Name)
  153. app.SetFocus(messageInputField)
  154. messages := util.GetMessages(session, currentChannel.ID, 50)
  155. for i := len(messages) - 1; i >= 0; i-- {
  156. util.WriteMessage(messagesTextView, session, messages[i])
  157. }
  158. }
  159. func onMessageInputFieldDone(key tcell.Key) {
  160. if key == tcell.KeyEnter {
  161. currentText := messageInputField.GetText()
  162. currentText = strings.TrimSpace(currentText)
  163. if currentText == "" {
  164. return
  165. }
  166. util.SendMessage(session, currentChannel.ID, currentText)
  167. messageInputField.SetText("")
  168. }
  169. }
  170. func onLoginFormLoginButtonSelected() {
  171. if loginVia == "emailpassword" {
  172. email := loginForm.GetFormItemByLabel("Email").(*tview.InputField).GetText()
  173. password := loginForm.GetFormItemByLabel("Password").(*tview.InputField).GetText()
  174. if email == "" || password == "" {
  175. return
  176. }
  177. session = newSession(email, password, "")
  178. util.SetPassword("token", session.Token)
  179. } else if loginVia == "token" {
  180. token := loginForm.GetFormItemByLabel("Token").(*tview.InputField).GetText()
  181. if token == "" {
  182. return
  183. }
  184. session = newSession("", "", token)
  185. util.SetPassword("token", token)
  186. }
  187. app.
  188. SetRoot(mainFlex, true).
  189. SetFocus(guildsDropDown)
  190. }