discordo.go 7.2 KB

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