123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package mux
- import (
- "fmt"
- "sort"
- "strconv"
- "git.mgmcomp.net/thisnthat/discordgo"
- )
- func (r *Router) helpCommandHandler(s *discordgo.Session, m *discordgo.Message, ctx *Context) {
- displayPrefix := ""
- switch ctx.Method {
- case DirectMethod:
- displayPrefix = ""
- case PrefixMethod:
- displayPrefix = r.prefix
- case MentionMethod:
- displayPrefix = fmt.Sprintf("@%s ", s.State.User.Username)
- }
- maxDisplayLen := 0
- cmdmap := make(map[string]*route)
- catMap := make(map[string][]string)
- var cats []string
- for _, v := range r.categories {
- keys := make([]string, 0, len(r.routes))
- for _, route := range r.routes {
- if route.CategoryID == v.ID {
- l := len(route.Usage) // TODO: Add the +args part :)
- if l > maxDisplayLen {
- maxDisplayLen = l
- }
- cmdmap[route.Name] = route
- keys = append(keys, route.Name)
- }
- }
- if len(keys) > 0 {
- sort.Strings(keys)
- catMap[v.Description] = keys
- cats = append(cats, v.Description)
- }
- }
- keys := make([]string, 0, len(r.routes))
- for _, route := range r.routes {
- if route.CategoryID == 0 {
- l := len(route.Usage) // TODO: Add the +args part :)
- if l > maxDisplayLen {
- maxDisplayLen = l
- }
- cmdmap[route.Name] = route
- if route.Name == "help" || route.Name == "about" {
- continue
- }
- keys = append(keys, route.Name)
- }
- }
- v, ok := cmdmap["help"]
- if ok {
- keys = append([]string{v.Name}, keys...)
- }
- v, ok = cmdmap["about"]
- if ok {
- keys = append([]string{v.Name}, keys...)
- }
- if len(keys) > 0 {
- sort.Strings(keys)
- catMap[""] = keys
- cats = append(cats, "")
- }
- maxDisplayLen = maxDisplayLen + 3
- // TODO: Learn more link needs to be configurable
- resp := "```autoit\n"
- // Add sorted result to help msg
- for _, catKey := range cats {
- if catKey != "" {
- resp += fmt.Sprintf("\n%s\n", catKey)
- } else {
- resp += fmt.Sprint("\n\n")
- }
- routes := catMap[catKey]
- for _, routeKey := range routes {
- route := cmdmap[routeKey]
- resp += fmt.Sprintf("%s%-"+strconv.Itoa(maxDisplayLen)+"s # %s\n", displayPrefix, route.Usage, route.Description)
- }
- }
- resp += "```\n"
- s.ChannelMessageSend(m.ChannelID, resp)
- return
- }
- func (r *Router) helpCommandHandlerOld(s *discordgo.Session, m *discordgo.Message, ctx *Context) {
- displayPrefix := ""
- switch ctx.Method {
- case DirectMethod:
- displayPrefix = ""
- case PrefixMethod:
- displayPrefix = r.prefix
- case MentionMethod:
- displayPrefix = fmt.Sprintf("@%s ", s.State.User.Username)
- }
- // Sort commands
- maxDisplayLen := 0
- keys := make([]string, 0, len(r.routes))
- cmdmap := make(map[string]*route)
- for _, v := range r.routes {
- // Only display commands with a description
- if v.Description == "" {
- continue
- }
- // Calculate the max length of command+args string
- l := len(v.Usage) // TODO: Add the +args part :)
- if l > maxDisplayLen {
- maxDisplayLen = l
- }
- cmdmap[v.Name] = v
- // help and about are added separately below.
- if v.Name == "help" || v.Name == "about" {
- continue
- }
- keys = append(keys, v.Name)
- }
- sort.Strings(keys)
- // TODO: Learn more link needs to be configurable
- resp := "```autoit\n"
- v, ok := cmdmap["help"]
- if ok {
- keys = append([]string{v.Name}, keys...)
- }
- v, ok = cmdmap["about"]
- if ok {
- keys = append([]string{v.Name}, keys...)
- }
- // Add sorted result to help msg
- for _, k := range keys {
- v := cmdmap[k]
- resp += fmt.Sprintf("%s%-"+strconv.Itoa(maxDisplayLen)+"s # %s\n", displayPrefix, v.Usage, v.Description)
- }
- resp += "```\n"
- s.ChannelMessageSend(m.ChannelID, resp)
- return
- }
|