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"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func PingCommand(ctx anpan.Context, _ []string) error {
|
func pingCommand(ctx anpan.Context, _ []string) error {
|
||||||
timeBefore := time.Now()
|
timeBefore := time.Now()
|
||||||
msg, _ := ctx.Reply("Pong!")
|
msg, _ := ctx.Reply("Pong!")
|
||||||
took := time.Now().Sub(timeBefore)
|
took := time.Now().Sub(timeBefore)
|
||||||
@ -19,7 +19,7 @@ func PingCommand(ctx anpan.Context, _ []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func SayCommand(ctx anpan.Context, args []string) error {
|
func sayCommand(ctx anpan.Context, args []string) error {
|
||||||
resp := strings.Join(args, " ")
|
resp := strings.Join(args, " ")
|
||||||
resp = strings.ReplaceAll(resp, "@everyone", "@\ufff0everyone")
|
resp = strings.ReplaceAll(resp, "@everyone", "@\ufff0everyone")
|
||||||
resp = strings.ReplaceAll(resp, "@here", "@\ufff0here")
|
resp = strings.ReplaceAll(resp, "@here", "@\ufff0here")
|
||||||
@ -27,7 +27,7 @@ func SayCommand(ctx anpan.Context, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func UserCommand(ctx anpan.Context, args []string) error {
|
func userCommand(ctx anpan.Context, args []string) error {
|
||||||
var member *discordgo.Member
|
var member *discordgo.Member
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
member, _ = ctx.Session.GuildMember(ctx.Guild.ID, ctx.Message.Author.ID)
|
member, _ = ctx.Session.GuildMember(ctx.Guild.ID, ctx.Message.Author.ID)
|
||||||
|
|||||||
@ -41,17 +41,12 @@ func main() {
|
|||||||
// check perms - bool
|
// check perms - bool
|
||||||
handler := anpan.NewCommandHandler(prefixes, owners, true, true)
|
handler := anpan.NewCommandHandler(prefixes, owners, true, true)
|
||||||
|
|
||||||
// Arguments:
|
// Add Command Handlers
|
||||||
// name - command name - string
|
exts.AddCommandHandlers(&handler)
|
||||||
// desc - command description - string
|
|
||||||
// owneronly - only allow owners to run - bool
|
if _, ok := handler.Commands["help"]; !ok {
|
||||||
// hidden - hide command from non-owners - bool
|
handler.AddDefaultHelpCommand()
|
||||||
// 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)
|
|
||||||
|
|
||||||
dg.AddHandler(handler.OnMessage)
|
dg.AddHandler(handler.OnMessage)
|
||||||
dg.AddHandler(handler.StatusHandler.OnReady)
|
dg.AddHandler(handler.StatusHandler.OnReady)
|
||||||
@ -68,5 +63,8 @@ func main() {
|
|||||||
<-sc
|
<-sc
|
||||||
|
|
||||||
fmt.Println("Shutting Down...")
|
fmt.Println("Shutting Down...")
|
||||||
dg.Close()
|
err = dg.Close()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user