discordo.go 5.9 KB

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