help.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. for _, v := range r.categories {
  23. keys := make([]string, 0, len(r.routes))
  24. for _, route := range r.routes {
  25. if route.CategoryID == v.ID {
  26. l := len(route.Usage) // TODO: Add the +args part :)
  27. if l > maxDisplayLen {
  28. maxDisplayLen = l
  29. }
  30. cmdmap[route.Name] = route
  31. keys = append(keys, route.Name)
  32. }
  33. }
  34. if len(keys) > 0 {
  35. sort.Strings(keys)
  36. catMap[v.Description] = keys
  37. cats = append(cats, v.Description)
  38. }
  39. }
  40. keys := make([]string, 0, len(r.routes))
  41. for _, route := range r.routes {
  42. if route.CategoryID == 0 {
  43. l := len(route.Usage) // TODO: Add the +args part :)
  44. if l > maxDisplayLen {
  45. maxDisplayLen = l
  46. }
  47. cmdmap[route.Name] = route
  48. if route.Name == "help" || route.Name == "about" {
  49. continue
  50. }
  51. keys = append(keys, route.Name)
  52. }
  53. }
  54. v, ok := cmdmap["help"]
  55. if ok {
  56. keys = append([]string{v.Name}, keys...)
  57. }
  58. v, ok = cmdmap["about"]
  59. if ok {
  60. keys = append([]string{v.Name}, keys...)
  61. }
  62. if len(keys) > 0 {
  63. sort.Strings(keys)
  64. catMap[""] = keys
  65. cats = append(cats, "")
  66. }
  67. maxDisplayLen = maxDisplayLen + 3
  68. // TODO: Learn more link needs to be configurable
  69. resp := "```autoit\n"
  70. // Add sorted result to help msg
  71. for _, catKey := range cats {
  72. if catKey != "" {
  73. resp += fmt.Sprintf("\n%s\n", catKey)
  74. } else {
  75. resp += fmt.Sprint("\n\n")
  76. }
  77. routes := catMap[catKey]
  78. for _, routeKey := range routes {
  79. route := cmdmap[routeKey]
  80. resp += fmt.Sprintf("%s%-"+strconv.Itoa(maxDisplayLen)+"s # %s\n", displayPrefix, route.Usage, route.Description)
  81. }
  82. }
  83. resp += "```\n"
  84. s.ChannelMessageSend(m.ChannelID, resp)
  85. return
  86. }
  87. func (r *Router) helpCommandHandlerOld(s *discordgo.Session, m *discordgo.Message, ctx *Context) {
  88. displayPrefix := ""
  89. switch ctx.Method {
  90. case DirectMethod:
  91. displayPrefix = ""
  92. case PrefixMethod:
  93. displayPrefix = r.prefix
  94. case MentionMethod:
  95. displayPrefix = fmt.Sprintf("@%s ", s.State.User.Username)
  96. }
  97. // Sort commands
  98. maxDisplayLen := 0
  99. keys := make([]string, 0, len(r.routes))
  100. cmdmap := make(map[string]*route)
  101. for _, v := range r.routes {
  102. // Only display commands with a description
  103. if v.Description == "" {
  104. continue
  105. }
  106. // Calculate the max length of command+args string
  107. l := len(v.Usage) // TODO: Add the +args part :)
  108. if l > maxDisplayLen {
  109. maxDisplayLen = l
  110. }
  111. cmdmap[v.Name] = v
  112. // help and about are added separately below.
  113. if v.Name == "help" || v.Name == "about" {
  114. continue
  115. }
  116. keys = append(keys, v.Name)
  117. }
  118. sort.Strings(keys)
  119. // TODO: Learn more link needs to be configurable
  120. resp := "```autoit\n"
  121. v, ok := cmdmap["help"]
  122. if ok {
  123. keys = append([]string{v.Name}, keys...)
  124. }
  125. v, ok = cmdmap["about"]
  126. if ok {
  127. keys = append([]string{v.Name}, keys...)
  128. }
  129. // Add sorted result to help msg
  130. for _, k := range keys {
  131. v := cmdmap[k]
  132. resp += fmt.Sprintf("%s%-"+strconv.Itoa(maxDisplayLen)+"s # %s\n", displayPrefix, v.Usage, v.Description)
  133. }
  134. resp += "```\n"
  135. s.ChannelMessageSend(m.ChannelID, resp)
  136. return
  137. }