handlers.go 8.3 KB

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