handlers.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. 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(util.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(util.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(util.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(util.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. app.MessagesTextView.Write(buildMessage(app, ms[i]))
  131. }
  132. app.MessagesTextView.ScrollToEnd()
  133. }()
  134. }
  135. func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  136. if app.SelectedChannel == nil {
  137. return nil
  138. }
  139. ms := app.SelectedChannel.Messages
  140. if len(ms) == 0 {
  141. return nil
  142. }
  143. switch e.Name() {
  144. case app.Config.Keybindings.SelectPreviousMessage:
  145. if len(app.MessagesTextView.GetHighlights()) == 0 {
  146. app.SelectedMessage = len(ms) - 1
  147. } else {
  148. app.SelectedMessage--
  149. if app.SelectedMessage < 0 {
  150. app.SelectedMessage = 0
  151. }
  152. }
  153. app.MessagesTextView.
  154. Highlight(ms[app.SelectedMessage].ID).
  155. ScrollToHighlight()
  156. return nil
  157. case app.Config.Keybindings.SelectNextMessage:
  158. if len(app.MessagesTextView.GetHighlights()) == 0 {
  159. app.SelectedMessage = len(ms) - 1
  160. } else {
  161. app.SelectedMessage++
  162. if app.SelectedMessage >= len(ms) {
  163. app.SelectedMessage = len(ms) - 1
  164. }
  165. }
  166. app.MessagesTextView.
  167. Highlight(ms[app.SelectedMessage].ID).
  168. ScrollToHighlight()
  169. return nil
  170. case app.Config.Keybindings.SelectFirstMessage:
  171. app.SelectedMessage = 0
  172. app.MessagesTextView.
  173. Highlight(ms[app.SelectedMessage].ID).
  174. ScrollToHighlight()
  175. return nil
  176. case app.Config.Keybindings.SelectLastMessage:
  177. app.SelectedMessage = len(ms) - 1
  178. app.MessagesTextView.
  179. Highlight(ms[app.SelectedMessage].ID).
  180. ScrollToHighlight()
  181. return nil
  182. case app.Config.Keybindings.ToggleMessageActionsList:
  183. messageActionsList := tview.NewList()
  184. hs := app.MessagesTextView.GetHighlights()
  185. if len(hs) == 0 {
  186. return nil
  187. }
  188. _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
  189. if m == nil {
  190. return nil
  191. }
  192. if util.HasPermission(app.Session.State, app.SelectedChannel.ID, discordgo.PermissionSendMessages) {
  193. messageActionsList.
  194. AddItem("Reply", "", 'r', nil).
  195. AddItem("Mention Reply", "", 'R', nil)
  196. }
  197. if m.ReferencedMessage != nil {
  198. messageActionsList.AddItem("Select Reply", "", 'm', nil)
  199. }
  200. messageActionsList.
  201. ShowSecondaryText(false).
  202. AddItem("Copy Content", "", 'c', nil).
  203. AddItem("Copy ID", "", 'i', nil).
  204. SetDoneFunc(func() {
  205. app.
  206. SetRoot(app.MainFlex, true).
  207. SetFocus(app.MessagesTextView)
  208. }).
  209. SetSelectedFunc(func(_ int, mainText string, _ string, _ rune) {
  210. onMessageActionsListSelected(app, mainText, m)
  211. }).
  212. SetTitle("Press the Escape key to close").
  213. SetBorder(true)
  214. app.SetRoot(messageActionsList, true)
  215. return nil
  216. }
  217. return e
  218. }
  219. func onMessageActionsListSelected(app *App, mainText string, m *discordgo.Message) {
  220. switch mainText {
  221. case "Copy Content":
  222. if err := clipboard.WriteAll(m.Content); err != nil {
  223. return
  224. }
  225. app.SetRoot(app.MainFlex, false)
  226. case "Copy ID":
  227. if err := clipboard.WriteAll(m.ID); err != nil {
  228. return
  229. }
  230. app.SetRoot(app.MainFlex, false)
  231. case "Reply":
  232. app.MessageInputField.SetTitle("Replying to " + m.Author.String())
  233. app.
  234. SetRoot(app.MainFlex, false).
  235. SetFocus(app.MessageInputField)
  236. case "Mention Reply":
  237. app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String())
  238. app.
  239. SetRoot(app.MainFlex, false).
  240. SetFocus(app.MessageInputField)
  241. case "Select Reply":
  242. app.SelectedMessage, _ = util.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID)
  243. app.MessagesTextView.
  244. Highlight(m.ReferencedMessage.ID).
  245. ScrollToHighlight()
  246. app.
  247. SetRoot(app.MainFlex, false).
  248. SetFocus(app.MessagesTextView)
  249. }
  250. }
  251. func onMessageInputFieldInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  252. switch e.Name() {
  253. case "Enter":
  254. if app.SelectedChannel == nil {
  255. return nil
  256. }
  257. t := strings.TrimSpace(app.MessageInputField.GetText())
  258. if t == "" {
  259. return nil
  260. }
  261. if len(app.MessagesTextView.GetHighlights()) != 0 {
  262. _, m := util.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0])
  263. d := &discordgo.MessageSend{
  264. Content: t,
  265. Reference: m.Reference(),
  266. AllowedMentions: &discordgo.MessageAllowedMentions{RepliedUser: false},
  267. }
  268. if strings.HasPrefix(app.MessageInputField.GetTitle(), "[@]") {
  269. d.AllowedMentions.RepliedUser = true
  270. } else {
  271. d.AllowedMentions.RepliedUser = false
  272. }
  273. go app.Session.ChannelMessageSendComplex(m.ChannelID, d)
  274. app.SelectedMessage = -1
  275. app.MessagesTextView.Highlight()
  276. app.MessageInputField.SetTitle("")
  277. } else {
  278. go app.Session.ChannelMessageSend(app.SelectedChannel.ID, t)
  279. }
  280. app.MessageInputField.SetText("")
  281. return nil
  282. case "Ctrl+V":
  283. text, _ := clipboard.ReadAll()
  284. text = app.MessageInputField.GetText() + text
  285. app.MessageInputField.SetText(text)
  286. return nil
  287. case "Esc":
  288. app.MessageInputField.
  289. SetText("").
  290. SetTitle("")
  291. app.SetFocus(app.MainFlex)
  292. app.SelectedMessage = -1
  293. app.MessagesTextView.Highlight()
  294. return nil
  295. case app.Config.Keybindings.ToggleExternalEditor:
  296. e := os.Getenv("EDITOR")
  297. if e == "" {
  298. return nil
  299. }
  300. f, err := os.CreateTemp(os.TempDir(), "discordo-*.md")
  301. if err != nil {
  302. return nil
  303. }
  304. defer os.Remove(f.Name())
  305. cmd := exec.Command(e, f.Name())
  306. cmd.Stdin = os.Stdin
  307. cmd.Stdout = os.Stdout
  308. app.Suspend(func() {
  309. err = cmd.Run()
  310. if err != nil {
  311. return
  312. }
  313. })
  314. b, err := io.ReadAll(f)
  315. if err != nil {
  316. return nil
  317. }
  318. app.MessageInputField.SetText(string(b))
  319. }
  320. return e
  321. }