help.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package mux
  2. import (
  3. "fmt"
  4. "sort"
  5. "strconv"
  6. "git.mgmcomp.net/thisnthat/discordgo"
  7. )
  8. func (r *Router) helpCommandHandler(s *discordgo.Session, m *discordgo.Message, ctx *Context) {
  9. displayPrefix := ""
  10. switch ctx.Method {
  11. case DirectMethod:
  12. displayPrefix = ""
  13. case PrefixMethod:
  14. displayPrefix = r.prefix
  15. case MentionMethod:
  16. displayPrefix = fmt.Sprintf("@%s ", s.State.User.Username)
  17. }
  18. maxDisplayLen := 0
  19. cmdmap := make(map[string]*route)
  20. catMap := make(map[string][]string)
  21. var cats []string
  22. var isAdmin = r.isAdminRoute(s, m, ctx)
  23. for _, v := range r.categories {
  24. keys := make([]string, 0, len(r.routes))
  25. for _, route := range r.routes {
  26. // If the route is hidde, do not add it to the help list
  27. if route.Hidden {
  28. continue
  29. }
  30. if route.Admin && !isAdmin {
  31. continue
  32. }
  33. if route.CategoryID == v.ID {
  34. l := len(route.Usage) // TODO: Add the +args part :)
  35. if l > maxDisplayLen {
  36. maxDisplayLen = l
  37. }
  38. cmdmap[route.Name] = route
  39. keys = append(keys, route.Name)
  40. }
  41. }
  42. if len(keys) > 0 {
  43. sort.Strings(keys)
  44. catMap[v.Description] = keys
  45. cats = append(cats, v.Description)
  46. }
  47. }
  48. keys := make([]string, 0, len(r.routes))
  49. for _, route := range r.routes {
  50. if route.CategoryID == 0 {
  51. if route.Admin && !isAdmin {
  52. continue
  53. }
  54. l := len(route.Usage) // TODO: Add the +args part :)
  55. if l > maxDisplayLen {
  56. maxDisplayLen = l
  57. }
  58. cmdmap[route.Name] = route
  59. keys = append(keys, route.Name)
  60. }
  61. }
  62. v, ok := cmdmap["help"]
  63. if ok {
  64. keys = append([]string{v.Name}, keys...)
  65. }
  66. v, ok = cmdmap["about"]
  67. if ok {
  68. keys = append([]string{v.Name}, keys...)
  69. }
  70. if len(keys) > 0 {
  71. sort.Strings(keys)
  72. catMap[""] = keys
  73. cats = append(cats, "")
  74. }
  75. maxDisplayLen = maxDisplayLen + 3
  76. // TODO: Learn more link needs to be configurable
  77. innerResp := ""
  78. // Add sorted result to help msg
  79. for _, catKey := range cats {
  80. if catKey != "" {
  81. innerResp += fmt.Sprintf("\n%s\n", catKey)
  82. } else {
  83. innerResp += "\n\n"
  84. }
  85. routes := catMap[catKey]
  86. for _, routeKey := range routes {
  87. route := cmdmap[routeKey]
  88. innerResp += fmt.Sprintf("%s%-"+strconv.Itoa(maxDisplayLen)+"s # %s\n", displayPrefix, route.Usage, route.Description)
  89. }
  90. }
  91. if innerResp == "" {
  92. return
  93. }
  94. resp := "```autoit\n" + innerResp + "```\n"
  95. s.ChannelMessageSend(m.ChannelID, resp)
  96. }