handlers.go 9.0 KB

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