handlers.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package ui
  2. import (
  3. "io"
  4. "os"
  5. "os/exec"
  6. "sort"
  7. "strings"
  8. "github.com/atotto/clipboard"
  9. "github.com/ayntgl/discordgo"
  10. "github.com/ayntgl/discordo/discord"
  11. "github.com/gdamore/tcell/v2"
  12. "github.com/rivo/tview"
  13. )
  14. func onAppInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  15. if app.MessageInputField.HasFocus() {
  16. return e
  17. }
  18. switch e.Name() {
  19. case app.Config.Keybindings.ToggleGuildsList:
  20. app.SetFocus(app.GuildsList)
  21. return nil
  22. case app.Config.Keybindings.ToggleChannelsTreeView:
  23. app.SetFocus(app.ChannelsTreeView)
  24. return nil
  25. case app.Config.Keybindings.ToggleMessagesTextView:
  26. app.SetFocus(app.MessagesTextView)
  27. return nil
  28. case app.Config.Keybindings.ToggleMessageInputField:
  29. app.SetFocus(app.MessageInputField)
  30. return nil
  31. }
  32. return e
  33. }
  34. func onGuildsListSelected(app *App, guildIdx int) {
  35. rootTreeNode := app.ChannelsTreeView.GetRoot()
  36. rootTreeNode.ClearChildren()
  37. app.SelectedMessage = -1
  38. app.MessagesTextView.
  39. Highlight().
  40. Clear().
  41. SetTitle("")
  42. app.MessageInputField.SetText("")
  43. // If the user is a bot account, the direct messages item does not exist in the guilds list.
  44. if app.Session.State.User.Bot && guildIdx == 0 {
  45. guildIdx = 1
  46. }
  47. if guildIdx == 0 { // Direct Messages
  48. cs := app.Session.State.PrivateChannels
  49. sort.Slice(cs, func(i, j int) bool {
  50. return cs[i].LastMessageID > cs[j].LastMessageID
  51. })
  52. for _, c := range cs {
  53. channelTreeNode := tview.NewTreeNode(channelToString(c)).
  54. SetReference(c.ID)
  55. rootTreeNode.AddChild(channelTreeNode)
  56. }
  57. } else { // Guild
  58. cs := app.Session.State.Guilds[guildIdx-1].Channels
  59. sort.Slice(cs, func(i, j int) bool {
  60. return cs[i].Position < cs[j].Position
  61. })
  62. for _, c := range cs {
  63. if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID == "") {
  64. channelTreeNode := tview.NewTreeNode(channelToString(c)).
  65. SetReference(c.ID)
  66. rootTreeNode.AddChild(channelTreeNode)
  67. }
  68. }
  69. CATEGORY:
  70. for _, c := range cs {
  71. if c.Type == discordgo.ChannelTypeGuildCategory {
  72. for _, nestedChannel := range cs {
  73. if nestedChannel.ParentID == c.ID {
  74. channelTreeNode := tview.NewTreeNode(c.Name).
  75. SetReference(c.ID)
  76. rootTreeNode.AddChild(channelTreeNode)
  77. continue CATEGORY
  78. }
  79. }
  80. channelTreeNode := tview.NewTreeNode(c.Name).
  81. SetReference(c.ID)
  82. rootTreeNode.AddChild(channelTreeNode)
  83. }
  84. }
  85. for _, c := range cs {
  86. if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID != "") {
  87. var parentTreeNode *tview.TreeNode
  88. rootTreeNode.Walk(func(node, _ *tview.TreeNode) bool {
  89. if node.GetReference() == c.ParentID {
  90. parentTreeNode = node
  91. return false
  92. }
  93. return true
  94. })
  95. if parentTreeNode != nil {
  96. channelTreeNode := tview.NewTreeNode(channelToString(c)).
  97. SetReference(c.ID)
  98. parentTreeNode.AddChild(channelTreeNode)
  99. }
  100. }
  101. }
  102. }
  103. app.ChannelsTreeView.SetCurrentNode(rootTreeNode)
  104. app.SetFocus(app.ChannelsTreeView)
  105. }
  106. func onChannelsTreeViewSelected(app *App, n *tview.TreeNode) {
  107. app.SelectedMessage = -1
  108. app.MessagesTextView.
  109. Highlight().
  110. Clear()
  111. app.MessageInputField.SetText("")
  112. c, err := app.Session.State.Channel(n.GetReference().(string))
  113. if err != nil {
  114. return
  115. }
  116. if c.Type == discordgo.ChannelTypeGuildCategory {
  117. n.SetExpanded(!n.IsExpanded())
  118. return
  119. }
  120. app.SelectedChannel = c
  121. app.MessagesTextView.SetTitle(channelToString(c))
  122. app.SetFocus(app.MessageInputField)
  123. go func() {
  124. ms, err := app.Session.ChannelMessages(c.ID, app.Config.General.FetchMessagesLimit, "", "", "")
  125. if err != nil {
  126. return
  127. }
  128. for i := len(ms) - 1; i >= 0; i-- {
  129. app.SelectedChannel.Messages = append(app.SelectedChannel.Messages, ms[i])
  130. _, err = app.MessagesTextView.Write(buildMessage(app, ms[i]))
  131. if err != nil {
  132. return
  133. }
  134. }
  135. app.MessagesTextView.ScrollToEnd()
  136. }()
  137. }
  138. func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  139. if app.SelectedChannel == nil {
  140. return nil
  141. }
  142. ms := app.SelectedChannel.Messages
  143. if len(ms) == 0 {
  144. return nil
  145. }
  146. switch e.Name() {
  147. case app.Config.Keybindings.SelectPreviousMessage:
  148. if len(app.MessagesTextView.GetHighlights()) == 0 {
  149. app.SelectedMessage = len(ms) - 1
  150. } else {
  151. app.SelectedMessage--
  152. if app.SelectedMessage < 0 {
  153. app.SelectedMessage = 0
  154. }
  155. }
  156. app.MessagesTextView.
  157. Highlight(ms[app.SelectedMessage].ID).
  158. ScrollToHighlight()
  159. return nil
  160. case app.Config.Keybindings.SelectNextMessage:
  161. if len(app.MessagesTextView.GetHighlights()) == 0 {
  162. app.SelectedMessage = len(ms) - 1
  163. } else {
  164. app.SelectedMessage++
  165. if app.SelectedMessage >= len(ms) {
  166. app.SelectedMessage = len(ms) - 1
  167. }
  168. }
  169. app.MessagesTextView.
  170. Highlight(ms[app.SelectedMessage].ID).
  171. ScrollToHighlight()
  172. return nil
  173. case app.Config.Keybindings.SelectFirstMessage:
  174. app.SelectedMessage = 0
  175. app.MessagesTextView.
  176. Highlight(ms[app.SelectedMessage].ID).
  177. ScrollToHighlight()
  178. return nil
  179. case app.Config.Keybindings.SelectLastMessage:
  180. app.SelectedMessage = len(ms) - 1
  181. app.MessagesTextView.
  182. Highlight(ms[app.SelectedMessage].ID).
  183. ScrollToHighlight()
  184. return nil
  185. case app.Config.Keybindings.ToggleMessageActionsList:
  186. messageActionsList := tview.NewList()
  187. hs := app.MessagesTextView.GetHighlights()
  188. if len(hs) == 0 {
  189. return nil
  190. }
  191. _, m := discord.FindMessageByID(app.SelectedChannel.Messages, hs[0])
  192. if m == nil {
  193. return nil
  194. }
  195. if discord.HasPermission(app.Session.State, app.SelectedChannel.ID, discordgo.PermissionSendMessages) {
  196. messageActionsList.
  197. AddItem("Reply", "", 'r', nil).
  198. AddItem("Mention Reply", "", 'R', nil)
  199. }
  200. if m.ReferencedMessage != nil {
  201. messageActionsList.AddItem("Select Reply", "", 'm', nil)
  202. }
  203. messageActionsList.
  204. ShowSecondaryText(false).
  205. AddItem("Copy Content", "", 'c', nil).
  206. AddItem("Copy ID", "", 'i', nil).
  207. SetDoneFunc(func() {
  208. app.
  209. SetRoot(app.MainFlex, true).
  210. SetFocus(app.MessagesTextView)
  211. }).
  212. SetSelectedFunc(func(_ int, mainText string, _ string, _ rune) {
  213. onMessageActionsListSelected(app, mainText, m)
  214. }).
  215. SetTitle("Press the Escape key to close").
  216. SetBorder(true)
  217. app.SetRoot(messageActionsList, true)
  218. return nil
  219. }
  220. return e
  221. }
  222. func onMessageActionsListSelected(app *App, mainText string, m *discordgo.Message) {
  223. switch mainText {
  224. case "Copy Content":
  225. if err := clipboard.WriteAll(m.Content); err != nil {
  226. return
  227. }
  228. app.SetRoot(app.MainFlex, false)
  229. case "Copy ID":
  230. if err := clipboard.WriteAll(m.ID); err != nil {
  231. return
  232. }
  233. app.SetRoot(app.MainFlex, false)
  234. case "Reply":
  235. app.MessageInputField.SetTitle("Replying to " + m.Author.String())
  236. app.
  237. SetRoot(app.MainFlex, false).
  238. SetFocus(app.MessageInputField)
  239. case "Mention Reply":
  240. app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String())
  241. app.
  242. SetRoot(app.MainFlex, false).
  243. SetFocus(app.MessageInputField)
  244. case "Select Reply":
  245. app.SelectedMessage, _ = discord.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID)
  246. app.MessagesTextView.
  247. Highlight(m.ReferencedMessage.ID).
  248. ScrollToHighlight()
  249. app.
  250. SetRoot(app.MainFlex, false).
  251. SetFocus(app.MessagesTextView)
  252. }
  253. }
  254. func onMessageInputFieldInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  255. switch e.Name() {
  256. case "Enter":
  257. if app.SelectedChannel == nil {
  258. return nil
  259. }
  260. t := strings.TrimSpace(app.MessageInputField.GetText())
  261. if t == "" {
  262. return nil
  263. }
  264. if len(app.MessagesTextView.GetHighlights()) != 0 {
  265. _, m := discord.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0])
  266. d := &discordgo.MessageSend{
  267. Content: t,
  268. Reference: m.Reference(),
  269. AllowedMentions: &discordgo.MessageAllowedMentions{RepliedUser: false},
  270. }
  271. if strings.HasPrefix(app.MessageInputField.GetTitle(), "[@]") {
  272. d.AllowedMentions.RepliedUser = true
  273. } else {
  274. d.AllowedMentions.RepliedUser = false
  275. }
  276. go app.Session.ChannelMessageSendComplex(m.ChannelID, d)
  277. app.SelectedMessage = -1
  278. app.MessagesTextView.Highlight()
  279. app.MessageInputField.SetTitle("")
  280. } else {
  281. go app.Session.ChannelMessageSend(app.SelectedChannel.ID, t)
  282. }
  283. app.MessageInputField.SetText("")
  284. return nil
  285. case "Ctrl+V":
  286. text, _ := clipboard.ReadAll()
  287. text = app.MessageInputField.GetText() + text
  288. app.MessageInputField.SetText(text)
  289. return nil
  290. case "Esc":
  291. app.MessageInputField.
  292. SetText("").
  293. SetTitle("")
  294. app.SetFocus(app.MainFlex)
  295. app.SelectedMessage = -1
  296. app.MessagesTextView.Highlight()
  297. return nil
  298. case app.Config.Keybindings.ToggleExternalEditor:
  299. e := os.Getenv("EDITOR")
  300. if e == "" {
  301. return nil
  302. }
  303. f, err := os.CreateTemp(os.TempDir(), "discordo-*.md")
  304. if err != nil {
  305. return nil
  306. }
  307. defer os.Remove(f.Name())
  308. cmd := exec.Command(e, f.Name())
  309. cmd.Stdin = os.Stdin
  310. cmd.Stdout = os.Stdout
  311. app.Suspend(func() {
  312. err = cmd.Run()
  313. if err != nil {
  314. return
  315. }
  316. })
  317. b, err := io.ReadAll(f)
  318. if err != nil {
  319. return nil
  320. }
  321. app.MessageInputField.SetText(string(b))
  322. return nil
  323. }
  324. return e
  325. }