handlers.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.ToggleGuildsList:
  17. app.SetFocus(app.GuildsList)
  18. return nil
  19. case app.Config.Keybindings.ToggleChannelsTreeView:
  20. app.SetFocus(app.ChannelsTreeView)
  21. return nil
  22. case app.Config.Keybindings.ToggleMessagesTextView:
  23. app.SetFocus(app.MessagesTextView)
  24. return nil
  25. case app.Config.Keybindings.ToggleMessageInputField:
  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.ToggleMessageActionsList:
  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.PermissionSendMessages) {
  184. messageActionsList.AddItem("Reply", "", 'r', nil)
  185. messageActionsList.AddItem("Mention Reply", "", 'R', nil)
  186. }
  187. if m.ReferencedMessage != nil {
  188. messageActionsList.AddItem("Select Reply", "", 'm', nil)
  189. }
  190. messageActionsList.
  191. ShowSecondaryText(false).
  192. AddItem("Copy Content", "", 'c', nil).
  193. AddItem("Copy ID", "", 'i', nil).
  194. SetDoneFunc(func() {
  195. app.
  196. SetRoot(app.MainFlex, true).
  197. SetFocus(app.MessagesTextView)
  198. }).
  199. SetSelectedFunc(func(_ int, mainText string, _ string, _ rune) {
  200. onMessageActionsListSelected(app, mainText, m)
  201. }).
  202. SetTitle("Press the Escape key to close").
  203. SetBorder(true)
  204. app.SetRoot(messageActionsList, true)
  205. }
  206. return e
  207. }
  208. func onMessageActionsListSelected(app *App, mainText string, m *discordgo.Message) {
  209. switch mainText {
  210. case "Copy Content":
  211. if err := clipboard.WriteAll(m.Content); err != nil {
  212. return
  213. }
  214. case "Copy ID":
  215. if err := clipboard.WriteAll(m.ID); err != nil {
  216. return
  217. }
  218. case "Reply":
  219. app.MessageInputField.SetTitle("Replying to " + m.Author.String())
  220. app.
  221. SetRoot(app.MainFlex, true).
  222. SetFocus(app.MessageInputField)
  223. case "Mention Reply":
  224. app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String())
  225. app.
  226. SetRoot(app.MainFlex, true).
  227. SetFocus(app.MessageInputField)
  228. case "Select Reply":
  229. app.SelectedMessage, _ = util.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID)
  230. app.MessagesTextView.
  231. Highlight(m.ReferencedMessage.ID).
  232. ScrollToHighlight()
  233. app.
  234. SetRoot(app.MainFlex, true).
  235. SetFocus(app.MessagesTextView)
  236. }
  237. }
  238. func onMessageInputFieldInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  239. switch e.Name() {
  240. case "Enter":
  241. if app.SelectedChannel == nil {
  242. return nil
  243. }
  244. t := strings.TrimSpace(app.MessageInputField.GetText())
  245. if t == "" {
  246. return nil
  247. }
  248. if len(app.MessagesTextView.GetHighlights()) != 0 {
  249. _, m := util.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0])
  250. d := &discordgo.MessageSend{
  251. Content: t,
  252. Reference: m.Reference(),
  253. AllowedMentions: &discordgo.MessageAllowedMentions{RepliedUser: false},
  254. }
  255. if strings.HasPrefix(app.MessageInputField.GetTitle(), "[@]") {
  256. d.AllowedMentions.RepliedUser = true
  257. } else {
  258. d.AllowedMentions.RepliedUser = false
  259. }
  260. go app.Session.ChannelMessageSendComplex(m.ChannelID, d)
  261. app.SelectedMessage = -1
  262. app.MessagesTextView.Highlight()
  263. app.MessageInputField.SetTitle("")
  264. } else {
  265. go app.Session.ChannelMessageSend(app.SelectedChannel.ID, t)
  266. }
  267. app.MessageInputField.SetText("")
  268. return nil
  269. case "Ctrl+V":
  270. text, _ := clipboard.ReadAll()
  271. text = app.MessageInputField.GetText() + text
  272. app.MessageInputField.SetText(text)
  273. return nil
  274. case "Esc":
  275. app.MessageInputField.
  276. SetText("").
  277. SetTitle("")
  278. app.SetFocus(app.MainFlex)
  279. app.SelectedMessage = -1
  280. app.MessagesTextView.Highlight()
  281. return nil
  282. case app.Config.Keybindings.ToggleExternalEditor:
  283. e := os.Getenv("EDITOR")
  284. if e == "" {
  285. return nil
  286. }
  287. f, err := os.CreateTemp(os.TempDir(), "discordo-*.md")
  288. if err != nil {
  289. return nil
  290. }
  291. defer os.Remove(f.Name())
  292. cmd := exec.Command(e, f.Name())
  293. cmd.Stdin = os.Stdin
  294. cmd.Stdout = os.Stdout
  295. app.Suspend(func() {
  296. err = cmd.Run()
  297. if err != nil {
  298. return
  299. }
  300. })
  301. b, err := io.ReadAll(f)
  302. if err != nil {
  303. return nil
  304. }
  305. app.MessageInputField.SetText(string(b))
  306. }
  307. return e
  308. }