handlers.go 8.5 KB

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