discordo.go 6.4 KB

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