Compare commits

..
5 Commits
3 changed files with 26 additions and 73 deletions
+20 -68
View File
@@ -9,10 +9,10 @@ package disgoman
import ( import (
"errors" "errors"
"fmt" "fmt"
"regexp"
"strings" "strings"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/kballard/go-shellquote"
) )
// AddCommand adds the Command at the address passed in to the Commands array on the CommandManager. // AddCommand adds the Command at the address passed in to the Commands array on the CommandManager.
@@ -91,31 +91,24 @@ func (c *CommandManager) OnMessage(session *discordgo.Session, m *discordgo.Mess
guild, _ := session.Guild(m.GuildID) guild, _ := session.Guild(m.GuildID)
ctx := Context{
Session: session,
Channel: channel,
Message: m.Message,
User: m.Author,
Guild: guild,
Member: m.Member,
Invoked: "",
CommandManager: c,
}
var cmd []string var cmd []string
// If we found our prefix then remove it and split the command into pieces // If we found our prefix then remove it and split the command into pieces
cmd, err = shellquote.Split(strings.TrimPrefix(content, prefix)) content = strings.TrimPrefix(content, prefix)
if err != nil { r := regexp.MustCompile(`[^ "]+|"([^"]*)"`)
fmt.Println(err.Error()) cmd = r.FindAllString(content, -1)
if strings.Contains(err.Error(), "Unterminated") { for i, val := range cmd {
cmd = strings.Split(strings.TrimPrefix(content, prefix), " ") cmd[i] = strings.Trim(val, "\"")
} else {
ctx := Context{
Session: session,
Channel: channel,
Message: m.Message,
User: m.Author,
Guild: guild,
Member: m.Member,
Invoked: "",
ErrorChannel: c.ErrorChannel,
}
c.ErrorChannel <- CommandError{
Context: ctx,
Message: "",
Error: err,
}
return
}
} }
if len(cmd) < 1 { if len(cmd) < 1 {
@@ -123,8 +116,8 @@ func (c *CommandManager) OnMessage(session *discordgo.Session, m *discordgo.Mess
} }
var command *Command var command *Command
invoked := cmd[0] ctx.Invoked = cmd[0]
if cmnd, ok := c.Commands[invoked]; ok { if cmnd, ok := c.Commands[ctx.Invoked]; ok {
command = cmnd command = cmnd
} else { } else {
fmt.Println("Command Not Found") fmt.Println("Command Not Found")
@@ -139,16 +132,6 @@ func (c *CommandManager) OnMessage(session *discordgo.Session, m *discordgo.Mess
} }
if !CheckPermissions(session, m.Author.ID, *channel, command.RequiredPermissions) { if !CheckPermissions(session, m.Author.ID, *channel, command.RequiredPermissions) {
ctx := Context{
Session: session,
Channel: channel,
Message: m.Message,
User: m.Author,
Guild: guild,
Member: m.Member,
Invoked: cmd[0],
ErrorChannel: c.ErrorChannel,
}
c.ErrorChannel <- CommandError{ c.ErrorChannel <- CommandError{
Context: ctx, Context: ctx,
Message: "You don't have the correct permissions to run this command.", Message: "You don't have the correct permissions to run this command.",
@@ -158,16 +141,6 @@ func (c *CommandManager) OnMessage(session *discordgo.Session, m *discordgo.Mess
} }
if !CheckPermissions(session, session.State.User.ID, *channel, command.RequiredPermissions) { if !CheckPermissions(session, session.State.User.ID, *channel, command.RequiredPermissions) {
ctx := Context{
Session: session,
Channel: channel,
Message: m.Message,
User: m.Author,
Guild: guild,
Member: m.Member,
Invoked: cmd[0],
ErrorChannel: c.ErrorChannel,
}
c.ErrorChannel <- CommandError{ c.ErrorChannel <- CommandError{
Context: ctx, Context: ctx,
Message: "I don't have the correct permissions to run this command.", Message: "I don't have the correct permissions to run this command.",
@@ -178,16 +151,6 @@ func (c *CommandManager) OnMessage(session *discordgo.Session, m *discordgo.Mess
} }
if command.OwnerOnly && !c.IsOwner(m.Author.ID) { if command.OwnerOnly && !c.IsOwner(m.Author.ID) {
ctx := Context{
Session: session,
Channel: channel,
Message: m.Message,
User: m.Author,
Guild: guild,
Member: m.Member,
Invoked: cmd[0],
ErrorChannel: c.ErrorChannel,
}
c.ErrorChannel <- CommandError{ c.ErrorChannel <- CommandError{
Context: ctx, Context: ctx,
Message: "Sorry, only the bot owner(s) can run that command!", Message: "Sorry, only the bot owner(s) can run that command!",
@@ -197,16 +160,5 @@ func (c *CommandManager) OnMessage(session *discordgo.Session, m *discordgo.Mess
} }
context := Context{ go command.Invoke(ctx, cmd[1:])
Session: session,
Channel: channel,
Message: m.Message,
User: m.Author,
Guild: guild,
Member: m.Member,
Invoked: invoked,
ErrorChannel: c.ErrorChannel,
}
go command.Invoke(context, cmd[1:])
} }
+4 -3
View File
@@ -1,8 +1,9 @@
package disgoman package disgoman
import ( import (
"github.com/bwmarrin/discordgo"
"io" "io"
"github.com/bwmarrin/discordgo"
) )
/* context.go: /* context.go:
@@ -32,8 +33,8 @@ func (c *Context) SendFile(filename string, file io.Reader) (*discordgo.Message,
// Will block if the channel buffer is full. It is up to the client to implement a channel for the errors as well as // Will block if the channel buffer is full. It is up to the client to implement a channel for the errors as well as
// a function to handle the errors from said channel. If the ErrorChannel is nil then this does nothing. // a function to handle the errors from said channel. If the ErrorChannel is nil then this does nothing.
func (c *Context) SendError(message string, err error) { func (c *Context) SendError(message string, err error) {
if c.ErrorChannel != nil { if c.CommandManager.ErrorChannel != nil {
c.ErrorChannel <- CommandError{ c.CommandManager.ErrorChannel <- CommandError{
Context: *c, Context: *c,
Message: message, Message: message,
Error: err, Error: err,
+2 -2
View File
@@ -80,6 +80,6 @@ type Context struct {
Member *discordgo.Member Member *discordgo.Member
// Name of the command as it was invoked (this is so you know what alias was used to call the command) // Name of the command as it was invoked (this is so you know what alias was used to call the command)
Invoked string Invoked string
// Error channel // Command Manager
ErrorChannel chan CommandError CommandManager *CommandManager
} }