discordo.go 5.8 KB

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