Add method to make adding commands easier
This commit is contained in:
parent
ba24958563
commit
e58aabae9a
17
djpianalto.com/goff/exts/init.go
Normal file
17
djpianalto.com/goff/exts/init.go
Normal file
@ -0,0 +1,17 @@
|
||||
package exts
|
||||
|
||||
import "github.com/MikeModder/anpan"
|
||||
|
||||
func AddCommandHandlers(h *anpan.CommandHandler) {
|
||||
// Arguments:
|
||||
// name - command name - string
|
||||
// desc - command description - string
|
||||
// owneronly - only allow owners to run - bool
|
||||
// hidden - hide command from non-owners - bool
|
||||
// perms - permissisions required - anpan.Permission (int)
|
||||
// type - command type, sets where the command is available
|
||||
// run - function to run - func(anpan.Context, []string) / CommandRunFunc
|
||||
h.AddCommand("ping", "Check the bot's ping", false, false, 0, anpan.CommandTypeEverywhere, pingCommand)
|
||||
h.AddCommand("say", "Repeat a message", false, false, 0, anpan.CommandTypeEverywhere, sayCommand)
|
||||
h.AddCommand("user", "Show info about a user", false, false, 0, anpan.CommandTypeEverywhere, userCommand)
|
||||
}
|
||||
@ -11,7 +11,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func PingCommand(ctx anpan.Context, _ []string) error {
|
||||
func pingCommand(ctx anpan.Context, _ []string) error {
|
||||
timeBefore := time.Now()
|
||||
msg, _ := ctx.Reply("Pong!")
|
||||
took := time.Now().Sub(timeBefore)
|
||||
@ -19,7 +19,7 @@ func PingCommand(ctx anpan.Context, _ []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func SayCommand(ctx anpan.Context, args []string) error {
|
||||
func sayCommand(ctx anpan.Context, args []string) error {
|
||||
resp := strings.Join(args, " ")
|
||||
resp = strings.ReplaceAll(resp, "@everyone", "@\ufff0everyone")
|
||||
resp = strings.ReplaceAll(resp, "@here", "@\ufff0here")
|
||||
@ -27,7 +27,7 @@ func SayCommand(ctx anpan.Context, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func UserCommand(ctx anpan.Context, args []string) error {
|
||||
func userCommand(ctx anpan.Context, args []string) error {
|
||||
var member *discordgo.Member
|
||||
if len(args) == 0 {
|
||||
member, _ = ctx.Session.GuildMember(ctx.Guild.ID, ctx.Message.Author.ID)
|
||||
|
||||
@ -41,17 +41,12 @@ func main() {
|
||||
// check perms - bool
|
||||
handler := anpan.NewCommandHandler(prefixes, owners, true, true)
|
||||
|
||||
// Arguments:
|
||||
// name - command name - string
|
||||
// desc - command description - string
|
||||
// owneronly - only allow owners to run - bool
|
||||
// hidden - hide command from non-owners - bool
|
||||
// perms - permissisions required - anpan.Permission (int)
|
||||
// type - command type, sets where the command is available
|
||||
// run - function to run - func(anpan.Context, []string) / CommandRunFunc
|
||||
handler.AddCommand("ping", "Check the bot's ping", false, false, 0, anpan.CommandTypeEverywhere, exts.PingCommand)
|
||||
handler.AddCommand("say", "Repeat a message", false, false, 0, anpan.CommandTypeEverywhere, exts.SayCommand)
|
||||
handler.AddCommand("user", "Show info about a user", false, false, 0, anpan.CommandTypeEverywhere, exts.UserCommand)
|
||||
// Add Command Handlers
|
||||
exts.AddCommandHandlers(&handler)
|
||||
|
||||
if _, ok := handler.Commands["help"]; !ok {
|
||||
handler.AddDefaultHelpCommand()
|
||||
}
|
||||
|
||||
dg.AddHandler(handler.OnMessage)
|
||||
dg.AddHandler(handler.StatusHandler.OnReady)
|
||||
@ -68,5 +63,8 @@ func main() {
|
||||
<-sc
|
||||
|
||||
fmt.Println("Shutting Down...")
|
||||
dg.Close()
|
||||
err = dg.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user