discordo.go 6.9 KB

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