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) } // 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.Name) // 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.Name, v.Description) } resp += "```\n" s.ChannelMessageSend(m.ChannelID, resp) return }