handlers.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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.MessagesTextView.
  38. Highlight().
  39. Clear().
  40. SetTitle("")
  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(util.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(util.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(util.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. c, err := app.Session.State.Channel(n.GetReference().(string))
  107. if err != nil {
  108. return
  109. }
  110. if c.Type == discordgo.ChannelTypeGuildCategory {
  111. n.SetExpanded(!n.IsExpanded())
  112. return
  113. }
  114. app.SelectedChannel = c
  115. app.MessagesTextView.SetTitle(util.ChannelToString(c))
  116. app.SetFocus(app.MessageInputField)
  117. go func() {
  118. ms, err := app.Session.ChannelMessages(c.ID, app.Config.General.FetchMessagesLimit, "", "", "")
  119. if err != nil {
  120. return
  121. }
  122. for i := len(ms) - 1; i >= 0; i-- {
  123. app.SelectedChannel.Messages = append(app.SelectedChannel.Messages, ms[i])
  124. app.MessagesTextView.Write(buildMessage(app, ms[i]))
  125. }
  126. app.MessagesTextView.ScrollToEnd()
  127. }()
  128. }
  129. func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  130. if app.SelectedChannel == nil {
  131. return nil
  132. }
  133. ms := app.SelectedChannel.Messages
  134. if len(ms) == 0 {
  135. return nil
  136. }
  137. switch e.Name() {
  138. case app.Config.Keybindings.SelectPreviousMessage:
  139. if len(app.MessagesTextView.GetHighlights()) == 0 {
  140. app.SelectedMessage = len(ms) - 1
  141. } else {
  142. app.SelectedMessage--
  143. if app.SelectedMessage < 0 {
  144. app.SelectedMessage = 0
  145. }
  146. }
  147. app.MessagesTextView.
  148. Highlight(ms[app.SelectedMessage].ID).
  149. ScrollToHighlight()
  150. return nil
  151. case app.Config.Keybindings.SelectNextMessage:
  152. if len(app.MessagesTextView.GetHighlights()) == 0 {
  153. app.SelectedMessage = len(ms) - 1
  154. } else {
  155. app.SelectedMessage++
  156. if app.SelectedMessage >= len(ms) {
  157. app.SelectedMessage = len(ms) - 1
  158. }
  159. }
  160. app.MessagesTextView.
  161. Highlight(ms[app.SelectedMessage].ID).
  162. ScrollToHighlight()
  163. return nil
  164. case app.Config.Keybindings.SelectFirstMessage:
  165. app.SelectedMessage = 0
  166. app.MessagesTextView.
  167. Highlight(ms[app.SelectedMessage].ID).
  168. ScrollToHighlight()
  169. return nil
  170. case app.Config.Keybindings.SelectLastMessage:
  171. app.SelectedMessage = len(ms) - 1
  172. app.MessagesTextView.
  173. Highlight(ms[app.SelectedMessage].ID).
  174. ScrollToHighlight()
  175. return nil
  176. case app.Config.Keybindings.ToggleMessageActionsList:
  177. messageActionsList := tview.NewList()
  178. hs := app.MessagesTextView.GetHighlights()
  179. if len(hs) == 0 {
  180. return nil
  181. }
  182. _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
  183. if m == nil {
  184. return 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. return nil
  209. }
  210. return e
  211. }
  212. func onMessageActionsListSelected(app *App, mainText string, m *discordgo.Message) {
  213. switch mainText {
  214. case "Copy Content":
  215. if err := clipboard.WriteAll(m.Content); err != nil {
  216. return
  217. }
  218. case "Copy ID":
  219. if err := clipboard.WriteAll(m.ID); err != nil {
  220. return
  221. }
  222. case "Reply":
  223. app.MessageInputField.SetTitle("Replying to " + m.Author.String())
  224. app.
  225. SetRoot(app.MainFlex, true).
  226. SetFocus(app.MessageInputField)
  227. case "Mention Reply":
  228. app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String())
  229. app.
  230. SetRoot(app.MainFlex, true).
  231. SetFocus(app.MessageInputField)
  232. case "Select Reply":
  233. app.SelectedMessage, _ = util.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID)
  234. app.MessagesTextView.
  235. Highlight(m.ReferencedMessage.ID).
  236. ScrollToHighlight()
  237. app.
  238. SetRoot(app.MainFlex, true).
  239. SetFocus(app.MessagesTextView)
  240. }
  241. }
  242. func onMessageInputFieldInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  243. switch e.Name() {
  244. case "Enter":
  245. if app.SelectedChannel == nil {
  246. return nil
  247. }
  248. t := strings.TrimSpace(app.MessageInputField.GetText())
  249. if t == "" {
  250. return nil
  251. }
  252. if len(app.MessagesTextView.GetHighlights()) != 0 {
  253. _, m := util.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0])
  254. d := &discordgo.MessageSend{
  255. Content: t,
  256. Reference: m.Reference(),
  257. AllowedMentions: &discordgo.MessageAllowedMentions{RepliedUser: false},
  258. }
  259. if strings.HasPrefix(app.MessageInputField.GetTitle(), "[@]") {
  260. d.AllowedMentions.RepliedUser = true
  261. } else {
  262. d.AllowedMentions.RepliedUser = false
  263. }
  264. go app.Session.ChannelMessageSendComplex(m.ChannelID, d)
  265. app.SelectedMessage = -1
  266. app.MessagesTextView.Highlight()
  267. app.MessageInputField.SetTitle("")
  268. } else {
  269. go app.Session.ChannelMessageSend(app.SelectedChannel.ID, t)
  270. }
  271. app.MessageInputField.SetText("")
  272. return nil
  273. case "Ctrl+V":
  274. text, _ := clipboard.ReadAll()
  275. text = app.MessageInputField.GetText() + text
  276. app.MessageInputField.SetText(text)
  277. return nil
  278. case "Esc":
  279. app.MessageInputField.
  280. SetText("").
  281. SetTitle("")
  282. app.SetFocus(app.MainFlex)
  283. app.SelectedMessage = -1
  284. app.MessagesTextView.Highlight()
  285. return nil
  286. case app.Config.Keybindings.ToggleExternalEditor:
  287. e := os.Getenv("EDITOR")
  288. if e == "" {
  289. return nil
  290. }
  291. f, err := os.CreateTemp(os.TempDir(), "discordo-*.md")
  292. if err != nil {
  293. return nil
  294. }
  295. defer os.Remove(f.Name())
  296. cmd := exec.Command(e, f.Name())
  297. cmd.Stdin = os.Stdin
  298. cmd.Stdout = os.Stdout
  299. app.Suspend(func() {
  300. err = cmd.Run()
  301. if err != nil {
  302. return
  303. }
  304. })
  305. b, err := io.ReadAll(f)
  306. if err != nil {
  307. return nil
  308. }
  309. app.MessageInputField.SetText(string(b))
  310. }
  311. return e
  312. }