grim/wasdead

Add a basic help system
draft
2019-04-08, Gary Kramlich
a883b88e2e8a
Parents f8a2c08e2a39
Children 3e3f0b135682
Add a basic help system
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/discord/command_help.go Mon Apr 08 23:26:38 2019 -0500
@@ -0,0 +1,55 @@
+package discord
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/bwmarrin/discordgo"
+)
+
+type commandHelp struct{}
+
+func (cmd *commandHelp) Help() string {
+ return "the amount of time the bot has been running"
+}
+
+func createField(name string, cmd Command) *discordgo.MessageEmbedField {
+ return &discordgo.MessageEmbedField{
+ Name: name,
+ Value: fmt.Sprintf("\nUsage: **%s**\n%s", name, cmd.Help()),
+ }
+
+}
+
+func (cmd *commandHelp) Run(args []string, c *DiscordClient, m *discordgo.MessageCreate) (interface{}, error) {
+ embed := &discordgo.MessageEmbed{
+ Author: &discordgo.MessageEmbedAuthor{
+ Name: "WasDead",
+ },
+ Description: "Command Help",
+ }
+
+ if len(args) == 0 {
+ for _, name := range commands.Keys() {
+ icmd, _ := commands.Get(name)
+
+ cmd := icmd.(Command)
+
+ embed.Fields = append(embed.Fields, createField(name, cmd))
+ }
+ } else if len(args) == 1 {
+ name := args[0]
+
+ icmd, found := commands.Get(strings.ToLower(name))
+ if !found {
+ return nil, fmt.Errorf("command %s not found", name)
+ }
+
+ cmd := icmd.(Command)
+ embed.Fields = append(embed.Fields, createField(name, cmd))
+ } else {
+ return nil, fmt.Errorf("unsupported number of arguments")
+ }
+
+ return embed, nil
+}
--- a/discord/commands.go Mon Apr 08 22:53:09 2019 -0500
+++ b/discord/commands.go Mon Apr 08 23:26:38 2019 -0500
@@ -18,6 +18,7 @@
)
func init() {
+ commands.Set("help", &commandHelp{})
commands.Set("set-channel", &commandSetChannel{})
commands.Set("status", &commandStatus{})
commands.Set("uptime", &commandUptime{})