handlers.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 the user is a bot account, the direct messages item does not exist in the guilds list.
  37. if app.Session.State.User.Bot && guildIdx == 0 {
  38. guildIdx = 1
  39. }
  40. if guildIdx == 0 { // Direct Messages
  41. cs := app.Session.State.PrivateChannels
  42. sort.Slice(cs, func(i, j int) bool {
  43. return cs[i].LastMessageID > cs[j].LastMessageID
  44. })
  45. for _, c := range cs {
  46. channelTreeNode := tview.NewTreeNode(util.ChannelToString(c)).
  47. SetReference(c.ID)
  48. rootTreeNode.AddChild(channelTreeNode)
  49. }
  50. } else { // Guild
  51. cs := app.Session.State.Guilds[guildIdx-1].Channels
  52. sort.Slice(cs, func(i, j int) bool {
  53. return cs[i].Position < cs[j].Position
  54. })
  55. for _, c := range cs {
  56. if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID == "") {
  57. channelTreeNode := tview.NewTreeNode(util.ChannelToString(c)).
  58. SetReference(c.ID)
  59. rootTreeNode.AddChild(channelTreeNode)
  60. }
  61. }
  62. CATEGORY:
  63. for _, c := range cs {
  64. if c.Type == discordgo.ChannelTypeGuildCategory {
  65. for _, nestedChannel := range cs {
  66. if nestedChannel.ParentID == c.ID {
  67. channelTreeNode := tview.NewTreeNode(c.Name).
  68. SetReference(c.ID)
  69. rootTreeNode.AddChild(channelTreeNode)
  70. continue CATEGORY
  71. }
  72. }
  73. channelTreeNode := tview.NewTreeNode(c.Name).
  74. SetReference(c.ID)
  75. rootTreeNode.AddChild(channelTreeNode)
  76. }
  77. }
  78. for _, c := range cs {
  79. if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID != "") {
  80. var parentTreeNode *tview.TreeNode
  81. rootTreeNode.Walk(func(node, _ *tview.TreeNode) bool {
  82. if node.GetReference() == c.ParentID {
  83. parentTreeNode = node
  84. return false
  85. }
  86. return true
  87. })
  88. if parentTreeNode != nil {
  89. channelTreeNode := tview.NewTreeNode(util.ChannelToString(c)).
  90. SetReference(c.ID)
  91. parentTreeNode.AddChild(channelTreeNode)
  92. }
  93. }
  94. }
  95. }
  96. app.ChannelsTreeView.SetCurrentNode(rootTreeNode)
  97. app.SetFocus(app.ChannelsTreeView)
  98. }
  99. func onChannelsTreeViewSelected(app *App, n *tview.TreeNode) {
  100. c, err := app.Session.State.Channel(n.GetReference().(string))
  101. if err != nil {
  102. return
  103. }
  104. if c.Type == discordgo.ChannelTypeGuildCategory {
  105. n.SetExpanded(!n.IsExpanded())
  106. return
  107. }
  108. app.SelectedChannel = c
  109. app.MessagesTextView.SetTitle(util.ChannelToString(c))
  110. app.SetFocus(app.MessageInputField)
  111. go func() {
  112. ms, err := app.Session.ChannelMessages(c.ID, app.Config.General.FetchMessagesLimit, "", "", "")
  113. if err != nil {
  114. return
  115. }
  116. for i := len(ms) - 1; i >= 0; i-- {
  117. app.SelectedChannel.Messages = append(app.SelectedChannel.Messages, ms[i])
  118. app.MessagesTextView.Write(buildMessage(app, ms[i]))
  119. }
  120. app.MessagesTextView.ScrollToEnd()
  121. }()
  122. }
  123. func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  124. if app.SelectedChannel == nil {
  125. return nil
  126. }
  127. ms := app.SelectedChannel.Messages
  128. if len(ms) == 0 {
  129. return nil
  130. }
  131. switch e.Name() {
  132. case app.Config.Keybindings.SelectPreviousMessage:
  133. if len(app.MessagesTextView.GetHighlights()) == 0 {
  134. app.SelectedMessage = len(ms) - 1
  135. } else {
  136. app.SelectedMessage--
  137. if app.SelectedMessage < 0 {
  138. app.SelectedMessage = 0
  139. }
  140. }
  141. app.MessagesTextView.
  142. Highlight(ms[app.SelectedMessage].ID).
  143. ScrollToHighlight()
  144. return nil
  145. case app.Config.Keybindings.SelectNextMessage:
  146. if len(app.MessagesTextView.GetHighlights()) == 0 {
  147. app.SelectedMessage = len(ms) - 1
  148. } else {
  149. app.SelectedMessage++
  150. if app.SelectedMessage >= len(ms) {
  151. app.SelectedMessage = len(ms) - 1
  152. }
  153. }
  154. app.MessagesTextView.
  155. Highlight(ms[app.SelectedMessage].ID).
  156. ScrollToHighlight()
  157. return nil
  158. case app.Config.Keybindings.SelectFirstMessage:
  159. app.SelectedMessage = 0
  160. app.MessagesTextView.
  161. Highlight(ms[app.SelectedMessage].ID).
  162. ScrollToHighlight()
  163. return nil
  164. case app.Config.Keybindings.SelectLastMessage:
  165. app.SelectedMessage = len(ms) - 1
  166. app.MessagesTextView.
  167. Highlight(ms[app.SelectedMessage].ID).
  168. ScrollToHighlight()
  169. return nil
  170. case app.Config.Keybindings.FocusMessageActionsList:
  171. messageActionsList := tview.NewList()
  172. hs := app.MessagesTextView.GetHighlights()
  173. if len(hs) == 0 {
  174. return nil
  175. }
  176. _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
  177. if m == nil {
  178. return nil
  179. }
  180. if util.HasPermission(app.Session.State, app.SelectedChannel.ID, discordgo.PermissionManageMessages) || m.Author.ID == app.Session.State.User.ID {
  181. messageActionsList.AddItem("Edit", "", 'e', 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. // The default global navigation shortcut for guilds list is Alt+<rune>.
  240. if e.Modifiers() == tcell.ModAlt {
  241. return nil
  242. }
  243. switch e.Key() {
  244. case tcell.KeyEnter:
  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 tcell.KeyCtrlV:
  274. text, _ := clipboard.ReadAll()
  275. text = app.MessageInputField.GetText() + text
  276. app.MessageInputField.SetText(text)
  277. return nil
  278. case tcell.KeyEscape:
  279. app.MessageInputField.SetText("")
  280. app.MessageInputField.SetTitle("")
  281. app.SelectedMessage = -1
  282. app.MessagesTextView.Highlight()
  283. return nil
  284. }
  285. return e
  286. }