Compare commits
No commits in common. "141acc6980f7c8d853aae9a15fcfe2b9a4b2553a" and "d3b95c693b4a4133a20a3d4fef8453eaf86b0dc9" have entirely different histories.
141acc6980
...
d3b95c693b
@ -1,87 +0,0 @@
|
|||||||
package bot
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.kill0.net/chill9/beepboop/lib"
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
DefaultCommander *Commander
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
Commander struct {
|
|
||||||
commands map[string]*Command
|
|
||||||
}
|
|
||||||
|
|
||||||
Command struct {
|
|
||||||
Name string
|
|
||||||
Config Config
|
|
||||||
Func func(cmd *Command, args []string) error
|
|
||||||
Session *discordgo.Session
|
|
||||||
Message *discordgo.MessageCreate
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
DefaultCommander = NewCommander()
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCommander() *Commander {
|
|
||||||
cmdr := new(Commander)
|
|
||||||
cmdr.commands = make(map[string]*Command)
|
|
||||||
return cmdr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cmdr *Commander) AddCommand(cmd *Command) {
|
|
||||||
cmdr.commands[cmd.Name] = cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cmdr *Commander) GetCommand(name string) (*Command, bool) {
|
|
||||||
cmd, ok := cmdr.commands[name]
|
|
||||||
return cmd, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddCommand(cmd *Command) {
|
|
||||||
DefaultCommander.AddCommand(cmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetCommand(name string) (*Command, bool) {
|
|
||||||
cmd, ok := DefaultCommander.GetCommand(name)
|
|
||||||
return cmd, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCommandHandler(config Config) func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
||||||
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
||||||
var cmd *Command
|
|
||||||
|
|
||||||
if m.Author.ID == s.State.User.ID {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !lib.HasCommand(m.Content, config.Prefix) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cmdName, args := lib.SplitCommandAndArgs(m.Content, config.Prefix)
|
|
||||||
|
|
||||||
cmd, ok := GetCommand(cmdName)
|
|
||||||
if ok {
|
|
||||||
cmd.Config = config
|
|
||||||
cmd.Name = cmdName
|
|
||||||
cmd.Session = s
|
|
||||||
cmd.Message = m
|
|
||||||
|
|
||||||
log.Debugf("command: %+v, args: %+v", cmd.Name, args)
|
|
||||||
cmd.Func(cmd, args)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Warnf("unknown command: %+v, args: %+v", cmdName, args)
|
|
||||||
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("unknown command: %s", cmdName))
|
|
||||||
}
|
|
||||||
}
|
|
@ -55,8 +55,6 @@ func main() {
|
|||||||
dg.AddHandler(h.Handle)
|
dg.AddHandler(h.Handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
dg.AddHandler(bot.NewCommandHandler(C))
|
|
||||||
|
|
||||||
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
||||||
|
|
||||||
err = dg.Open()
|
err = dg.Open()
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import "testing"
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestContainsCommand(t *testing.T) {
|
func TestContainsCommand(t *testing.T) {
|
||||||
tables := []struct {
|
tables := []struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user