discordo.go 5.8 KB

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