basic command system

This commit is contained in:
Cynthia Foxwell 2023-07-08 14:51:26 -06:00
parent c70359d412
commit 10ba1af405
10 changed files with 201 additions and 17 deletions

36
commands/help.go Normal file
View file

@ -0,0 +1,36 @@
package commands
import (
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/fatih/color"
)
func HelpCommand(session *discordgo.Session) {
fmt.Println("\r\nCOMcord (c)left 2023\n\r")
index := 0
for key, cmd := range GetAllCommands() {
str := fmt.Sprintf(" %s - %s", key, cmd.Description)
length := len(str)
fmt.Print(" ")
color.Set(color.FgYellow, color.Bold)
fmt.Print(key)
color.Unset()
fmt.Printf(" - %s", cmd.Description)
fmt.Print(strings.Repeat(" ", 25 - length))
index++
if index % 3 == 0 {
fmt.Print("\n\r")
}
}
if index % 3 != 0 {
fmt.Print("\n\r")
}
fmt.Println("\r\nTo begin TALK MODE, press [SPACE]\n\r")
}

33
commands/main.go Normal file
View file

@ -0,0 +1,33 @@
package commands
import "github.com/bwmarrin/discordgo"
var commandMap map[string]Command
type Command struct {
Run func(*discordgo.Session)
Description string
}
func Setup() {
commandMap = make(map[string]Command)
commandMap["q"] = Command{
Run: QuitCommand,
Description: "quit comcord",
}
commandMap["h"] = Command{
Run: HelpCommand,
Description: "command help",
}
}
func GetCommand(key string) (Command, bool) {
command, has := commandMap[key]
return command, has
}
func GetAllCommands() map[string]Command {
return commandMap
}

12
commands/quit.go Normal file
View file

@ -0,0 +1,12 @@
package commands
import (
"os"
"github.com/bwmarrin/discordgo"
)
func QuitCommand(session *discordgo.Session) {
session.Close()
os.Exit(0)
}