help.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // Sort commands
  19. maxDisplayLen := 0
  20. keys := make([]string, 0, len(r.routes))
  21. cmdmap := make(map[string]*route)
  22. for _, v := range r.routes {
  23. // Only display commands with a description
  24. if v.Description == "" {
  25. continue
  26. }
  27. // Calculate the max length of command+args string
  28. l := len(v.Name) // TODO: Add the +args part :)
  29. if l > maxDisplayLen {
  30. maxDisplayLen = l
  31. }
  32. cmdmap[v.Name] = v
  33. // help and about are added separately below.
  34. if v.Name == "help" || v.Name == "about" {
  35. continue
  36. }
  37. keys = append(keys, v.Name)
  38. }
  39. sort.Strings(keys)
  40. // TODO: Learn more link needs to be configurable
  41. resp := "```autoit\n"
  42. v, ok := cmdmap["help"]
  43. if ok {
  44. keys = append([]string{v.Name}, keys...)
  45. }
  46. v, ok = cmdmap["about"]
  47. if ok {
  48. keys = append([]string{v.Name}, keys...)
  49. }
  50. // Add sorted result to help msg
  51. for _, k := range keys {
  52. v := cmdmap[k]
  53. resp += fmt.Sprintf("%s%-"+strconv.Itoa(maxDisplayLen)+"s # %s\n", displayPrefix, v.Name, v.Description)
  54. }
  55. resp += "```\n"
  56. s.ChannelMessageSend(m.ChannelID, resp)
  57. return
  58. }