discordo.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. config *util.Config
  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. config = util.NewConfig()
  41. loginModal = ui.NewLoginModal(onLoginModalDone)
  42. guildsDropDown = ui.NewGuildsDropDown(onGuildsDropDownSelected, config.Theme)
  43. channelsTreeNode = ui.NewChannelsTreeNode()
  44. channelsTreeView = ui.NewChannelsTreeView(channelsTreeNode, onChannelsTreeViewSelected, config.Theme)
  45. messagesTextView = ui.NewMessagesTextView(onMessagesTextViewChanged, config.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. var sess *discordgo.Session
  80. var err error
  81. if email != "" && password != "" {
  82. sess, err = discordgo.New(email, password)
  83. if err != nil {
  84. panic(err)
  85. }
  86. } else if token != "" {
  87. sess, err = discordgo.New(token)
  88. if err != nil {
  89. panic(err)
  90. }
  91. }
  92. sess.UserAgent = "" +
  93. "Mozilla/5.0 (X11; Linux x86_64) " +
  94. "AppleWebKit/537.36 (KHTML, like Gecko) " +
  95. "Chrome/91.0.4472.164 Safari/537.36"
  96. sess.Identify.Properties.Browser = "Chrome"
  97. sess.Identify.Properties.OS = "Linux"
  98. sess.AddHandler(onReady)
  99. sess.AddHandler(onGuildCreate)
  100. sess.AddHandler(onMessageCreate)
  101. if err = sess.Open(); err != nil {
  102. panic(err)
  103. }
  104. return sess
  105. }
  106. func onGuildCreate(_ *discordgo.Session, guild *discordgo.GuildCreate) {
  107. guildsDropDown.AddOption(guild.Name, nil)
  108. }
  109. func onReady(_ *discordgo.Session, ready *discordgo.Ready) {
  110. for i := range ready.Guilds {
  111. guildsDropDown.AddOption(ready.Guilds[i].Name, nil)
  112. }
  113. }
  114. func onMessageCreate(_ *discordgo.Session, message *discordgo.MessageCreate) {
  115. if currentChannel != nil && currentChannel.ID == message.ChannelID {
  116. util.WriteMessage(messagesTextView, session, message.Message)
  117. }
  118. }
  119. func onGuildsDropDownSelected(text string, _ int) {
  120. channelsTreeNode.ClearChildren()
  121. messagesTextView.Clear()
  122. if messageInputField != nil {
  123. mainFlex.RemoveItem(messageInputField)
  124. messageInputField = nil
  125. }
  126. guilds := session.State.Guilds
  127. for i := range guilds {
  128. guild := guilds[i]
  129. if guild.Name == text {
  130. currentGuild = guild
  131. break
  132. }
  133. }
  134. channelsTreeView.SetTitle("Channels")
  135. app.SetFocus(channelsTreeView)
  136. channels := currentGuild.Channels
  137. sort.Slice(channels, func(i, j int) bool {
  138. return channels[i].Position < channels[j].Position
  139. })
  140. for i := range channels {
  141. channel := channels[i]
  142. channelNode := tview.NewTreeNode(channel.Name).
  143. SetReference(channel)
  144. switch channel.Type {
  145. case discordgo.ChannelTypeGuildCategory:
  146. channelNode.SetColor(tcell.GetColor(config.Theme.TreeNodeForeground))
  147. channelsTreeNode.AddChild(channelNode)
  148. case discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews:
  149. if channel.ParentID == "" {
  150. channelNode.SetText("[::d]#" + channel.Name + "[-:-:-]")
  151. channelsTreeNode.AddChild(channelNode)
  152. }
  153. }
  154. }
  155. }
  156. func onChannelsTreeViewSelected(node *tview.TreeNode) {
  157. messagesTextView.Clear()
  158. if messageInputField == nil {
  159. messageInputField = ui.NewMessageInputField(onMessageInputFieldDone, config.Theme)
  160. mainFlex.AddItem(messageInputField, 3, 1, false)
  161. }
  162. currentChannel = node.GetReference().(*discordgo.Channel)
  163. switch currentChannel.Type {
  164. case discordgo.ChannelTypeGuildCategory:
  165. if len(node.GetChildren()) == 0 {
  166. for i := range currentGuild.Channels {
  167. channel := currentGuild.Channels[i]
  168. if (channel.Type == discordgo.ChannelTypeGuildText || channel.Type == discordgo.ChannelTypeGuildNews) && channel.ParentID == currentChannel.ID {
  169. channelNode := tview.NewTreeNode("[::d]#" + channel.Name + "[-:-:-]").
  170. SetReference(channel)
  171. node.AddChild(channelNode)
  172. }
  173. }
  174. } else {
  175. node.SetExpanded(!node.IsExpanded())
  176. }
  177. case discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews:
  178. messagesTextView.SetTitle(currentChannel.Name)
  179. app.SetFocus(messageInputField)
  180. messages, err := session.ChannelMessages(currentChannel.ID, config.GetMessagesLimit, "", "", "")
  181. if err != nil {
  182. panic(err)
  183. }
  184. for i := len(messages) - 1; i >= 0; i-- {
  185. util.WriteMessage(messagesTextView, session, messages[i])
  186. }
  187. }
  188. }
  189. func onMessageInputFieldDone(key tcell.Key) {
  190. if key == tcell.KeyEnter {
  191. currentText := messageInputField.GetText()
  192. currentText = strings.TrimSpace(currentText)
  193. if currentText == "" {
  194. return
  195. }
  196. _, err := session.ChannelMessageSend(currentChannel.ID, currentText)
  197. if err != nil {
  198. panic(err)
  199. }
  200. messageInputField.SetText("")
  201. }
  202. }
  203. func onLoginFormLoginButtonSelected() {
  204. if loginVia == "emailpassword" {
  205. email := loginForm.GetFormItemByLabel("Email").(*tview.InputField).GetText()
  206. password := loginForm.GetFormItemByLabel("Password").(*tview.InputField).GetText()
  207. if email == "" || password == "" {
  208. return
  209. }
  210. session = newSession(email, password, "")
  211. util.SetPassword("token", session.Token)
  212. } else if loginVia == "token" {
  213. token := loginForm.GetFormItemByLabel("Token").(*tview.InputField).GetText()
  214. if token == "" {
  215. return
  216. }
  217. session = newSession("", "", token)
  218. util.SetPassword("token", token)
  219. }
  220. app.
  221. SetRoot(mainFlex, true).
  222. SetFocus(guildsDropDown)
  223. }