discordo.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package main
  2. import (
  3. "strings"
  4. "github.com/diamondburned/arikawa/v2/api"
  5. "github.com/diamondburned/arikawa/v2/discord"
  6. "github.com/diamondburned/arikawa/v2/gateway"
  7. "github.com/diamondburned/arikawa/v2/session"
  8. "github.com/gdamore/tcell/v2"
  9. "github.com/rigormorrtiss/discordo/ui"
  10. "github.com/rigormorrtiss/discordo/util"
  11. "github.com/rivo/tview"
  12. )
  13. var app *tview.Application
  14. var loginModal *tview.Modal
  15. var loginForm *tview.Form
  16. var guildsDropDown *tview.DropDown
  17. var channelsList *tview.List
  18. var messagesTextView *tview.TextView
  19. var messageInputField *tview.InputField
  20. var mainFlex *tview.Flex
  21. var loginVia string
  22. var discordSession *session.Session
  23. var guilds []gateway.GuildCreateEvent
  24. var currentGuild gateway.GuildCreateEvent
  25. var currentChannel discord.Channel
  26. func main() {
  27. loginModal = ui.NewLoginModal(onLoginModalDone)
  28. guildsDropDown = ui.NewGuildsDropDown(onGuildsDropDownSelected)
  29. channelsList = ui.NewChannelsList(onChannelsListSelected)
  30. messagesTextView = ui.NewMessagesTextView(onMessagesTextViewChanged)
  31. mainFlex = ui.NewMainFlex(guildsDropDown, channelsList, messagesTextView)
  32. app = ui.NewApplication(onApplicationInputCapture)
  33. token := util.GetPassword("token")
  34. if token != "" {
  35. app.
  36. SetRoot(mainFlex, true).
  37. SetFocus(guildsDropDown)
  38. discordSession = newSession("", "", token)
  39. } else {
  40. app.SetRoot(loginModal, true)
  41. }
  42. if err := app.Run(); err != nil {
  43. panic(err)
  44. }
  45. }
  46. func onLoginFormQuitButtonSelected() {
  47. app.Stop()
  48. }
  49. func onApplicationInputCapture(event *tcell.EventKey) *tcell.EventKey {
  50. if event.Key() == tcell.KeyCtrlR {
  51. app.Sync()
  52. }
  53. return event
  54. }
  55. func onMessagesTextViewChanged() {
  56. app.Draw()
  57. }
  58. func onLoginModalDone(buttonIndex int, buttonLabel string) {
  59. if buttonLabel == ui.LoginViaEmailPasswordLoginModalButton {
  60. loginVia = "emailpassword"
  61. loginForm = ui.NewLoginForm(loginVia, onLoginFormLoginButtonSelected, onLoginFormQuitButtonSelected)
  62. app.SetRoot(loginForm, true)
  63. } else if buttonLabel == ui.LoginViaTokenLoginModalButton {
  64. loginVia = "token"
  65. loginForm = ui.NewLoginForm(loginVia, onLoginFormLoginButtonSelected, onLoginFormQuitButtonSelected)
  66. app.SetRoot(loginForm, true)
  67. }
  68. }
  69. func newSession(email string, password string, token string) *session.Session {
  70. var sess *session.Session
  71. var err error
  72. if email != "" && password != "" {
  73. api.UserAgent = `Mozilla/5.0 (X11; Linux x86_64; rv:90.0)` +
  74. `Gecko/20100101 Firefox/90.0`
  75. gateway.DefaultIdentity = gateway.IdentifyProperties{
  76. OS: "Linux",
  77. Browser: "Firefox",
  78. Device: "",
  79. }
  80. sess, err = session.Login(email, password, "")
  81. if err != nil {
  82. panic(err)
  83. }
  84. sess.AddHandler(onReady)
  85. } else if token != "" {
  86. sess, err = session.New(token)
  87. if err != nil {
  88. panic(err)
  89. }
  90. sess.AddHandler(onGuildCreate)
  91. sess.Gateway.AddIntents(gateway.IntentGuilds)
  92. sess.Gateway.AddIntents(gateway.IntentGuildMessages)
  93. }
  94. sess.AddHandler(onMessageCreate)
  95. if err = sess.Open(); err != nil {
  96. panic(err)
  97. }
  98. return sess
  99. }
  100. func onGuildCreate(guild *gateway.GuildCreateEvent) {
  101. guildsDropDown.AddOption(guild.Name, nil)
  102. guilds = append(guilds, *guild)
  103. }
  104. func onReady(ready *gateway.ReadyEvent) {
  105. guilds = ready.Guilds
  106. for i := 0; i < len(guilds); i++ {
  107. guildsDropDown.AddOption(guilds[i].Name, nil)
  108. }
  109. }
  110. func onMessageCreate(message *gateway.MessageCreateEvent) {
  111. if currentChannel.ID == message.ChannelID {
  112. util.WriteMessage(messagesTextView, message.Message)
  113. }
  114. }
  115. func onGuildsDropDownSelected(text string, _ int) {
  116. // Remove/clear all items from the channels List
  117. channelsList.Clear()
  118. // Remove/clear all text from the messages TextView buffer
  119. messagesTextView.Clear()
  120. // If the message InputField is not nil, remove the message InputField from the main Flex and set the message InputField to nil
  121. if messageInputField != nil {
  122. mainFlex.RemoveItem(messageInputField)
  123. messageInputField = nil
  124. }
  125. for i := 0; i < len(guilds); i++ {
  126. guild := guilds[i]
  127. if guild.Name == text {
  128. currentGuild = guild
  129. break
  130. }
  131. }
  132. for i := 0; i < len(currentGuild.Channels); i++ {
  133. channel := currentGuild.Channels[i]
  134. channelsList.AddItem(channel.Name, "", 0, nil)
  135. }
  136. app.SetFocus(channelsList)
  137. }
  138. func onChannelsListSelected(i int, mainText string, secondaryText string, _ rune) {
  139. // Remove/clear all text from the messages TextView buffer
  140. messagesTextView.Clear()
  141. // If the message InputField is nil, add a new message InputField to the main Flex and assign it to message InputField in instance
  142. if messageInputField == nil {
  143. messageInputField = ui.NewMessageInputField(onMessageInputFieldDone)
  144. // Add the message InputField as a new item to the main Flex
  145. mainFlex.AddItem(messageInputField, 3, 1, false)
  146. }
  147. app.SetFocus(messageInputField)
  148. currentChannel = currentGuild.Channels[i]
  149. // Set the title of the messages TextView Box to the name of the channel
  150. messagesTextView.SetTitle(currentChannel.Name)
  151. messages := util.GetMessages(discordSession, currentChannel.ID, 50)
  152. for i := len(messages) - 1; i >= 0; i-- {
  153. util.WriteMessage(messagesTextView, messages[i])
  154. }
  155. }
  156. func onMessageInputFieldDone(key tcell.Key) {
  157. if key == tcell.KeyEnter {
  158. currentText := messageInputField.GetText()
  159. currentText = strings.TrimSpace(currentText)
  160. // If the current text of the message InputField is an empty string and the enter key is pressed, do not proceed
  161. if currentText == "" {
  162. return
  163. }
  164. util.SendMessage(discordSession, currentChannel.ID, currentText)
  165. // Set the current text of the message InputField to an empty string after the message has been sent
  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. discordSession = newSession(email, password, "")
  177. util.SetPassword("token", discordSession.Token)
  178. } else if loginVia == "token" {
  179. token := loginForm.GetFormItemByLabel("Token").(*tview.InputField).GetText()
  180. if token == "" {
  181. return
  182. }
  183. discordSession = newSession("", "", token)
  184. util.SetPassword("token", token)
  185. }
  186. app.
  187. SetRoot(mainFlex, true).
  188. SetFocus(guildsDropDown)
  189. }