handlers.go 9.1 KB

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