util.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package main
  2. import (
  3. "github.com/ayntgl/discordgo"
  4. "github.com/ayntgl/discordo/util"
  5. "github.com/rivo/tview"
  6. )
  7. func createTopLevelChannelsTreeNodes(
  8. n *tview.TreeNode,
  9. cs []*discordgo.Channel,
  10. ) {
  11. for _, c := range cs {
  12. if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) &&
  13. (c.ParentID == "") {
  14. p, err := session.State.UserChannelPermissions(session.State.User.ID, c.ID)
  15. if err != nil || p&discordgo.PermissionViewChannel != discordgo.PermissionViewChannel {
  16. continue
  17. }
  18. var tag string
  19. if util.ChannelIsUnread(session.State, c) {
  20. tag = "[::b]"
  21. } else {
  22. tag = "[::d]"
  23. }
  24. cn := tview.NewTreeNode(tag + util.ChannelToString(c) + "[::-]").
  25. SetReference(c.ID)
  26. n.AddChild(cn)
  27. continue
  28. }
  29. }
  30. }
  31. func createCategoryChannelsTreeNodes(
  32. n *tview.TreeNode,
  33. cs []*discordgo.Channel,
  34. ) {
  35. CategoryLoop:
  36. for _, c := range cs {
  37. if c.Type == discordgo.ChannelTypeGuildCategory {
  38. p, err := session.State.UserChannelPermissions(session.State.User.ID, c.ID)
  39. if err != nil || p&discordgo.PermissionViewChannel != discordgo.PermissionViewChannel {
  40. continue
  41. }
  42. for _, child := range cs {
  43. if child.ParentID == c.ID {
  44. cn := tview.NewTreeNode(c.Name).
  45. SetReference(c.ID)
  46. n.AddChild(cn)
  47. continue CategoryLoop
  48. }
  49. }
  50. cn := tview.NewTreeNode(c.Name).
  51. SetReference(c.ID)
  52. n.AddChild(cn)
  53. }
  54. }
  55. }
  56. func createSecondLevelChannelsTreeNodes(cs []*discordgo.Channel) {
  57. for _, c := range cs {
  58. if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) &&
  59. (c.ParentID != "") {
  60. p, err := session.State.UserChannelPermissions(session.State.User.ID, c.ID)
  61. if err != nil || p&discordgo.PermissionViewChannel != discordgo.PermissionViewChannel {
  62. continue
  63. }
  64. var tag string
  65. if util.ChannelIsUnread(session.State, c) {
  66. tag = "[::b]"
  67. } else {
  68. tag = "[::d]"
  69. }
  70. pn := util.GetTreeNodeByReference(channelsTree, c.ParentID)
  71. if pn != nil {
  72. cn := tview.NewTreeNode(tag + util.ChannelToString(c) + "[::-]").
  73. SetReference(c.ID)
  74. pn.AddChild(cn)
  75. }
  76. }
  77. }
  78. }