Update command manager to use the error channel

master
DustyP 6 years ago
parent 662acb500b
commit 3fc81a6451

@ -11,7 +11,6 @@ import (
"fmt" "fmt"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/kballard/go-shellquote" "github.com/kballard/go-shellquote"
"log"
"strings" "strings"
) )
@ -83,10 +82,32 @@ func (c *CommandManager) OnMessage(session *discordgo.Session, m *discordgo.Mess
return // If we didn't find a valid prefix then exit return // If we didn't find a valid prefix then exit
} }
channel, err := session.Channel(m.ChannelID)
if err != nil {
fmt.Println("Couldn't retrieve Channel.")
return
}
guild, _ := session.Guild(m.GuildID)
// 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)) cmd, err := shellquote.Split(strings.TrimPrefix(content, prefix))
if err != nil { if err != nil {
log.Fatal(err) ctx := Context{
Session: session,
Channel: channel,
Message: m.Message,
User: m.Author,
Guild: guild,
Member: m.Member,
Invoked: nil,
ErrorChannel: c.ErrorChannel,
}
c.ErrorChannel <- CommandError{
Context: ctx,
Message: "",
Error: err,
}
return return
} }
@ -99,65 +120,64 @@ func (c *CommandManager) OnMessage(session *discordgo.Session, m *discordgo.Mess
return return
} }
channel, err := session.Channel(m.ChannelID)
if err != nil {
fmt.Println("Couldn't retrieve Channel.")
return
}
if !CheckPermissions(session, m.Author.ID, *channel, command.RequiredPermissions) { if !CheckPermissions(session, m.Author.ID, *channel, command.RequiredPermissions) {
embed := &discordgo.MessageEmbed{ ctx := Context{
Title: "Insufficient Permissions", Session: session,
Description: "You don't have the correct permissions to run this command.", Channel: channel,
Color: 0xFF0000, Message: m.Message,
User: m.Author,
Guild: guild,
Member: m.Member,
Invoked: cmd[0],
ErrorChannel: c.ErrorChannel,
} }
if !command.Hidden { c.ErrorChannel <- CommandError{
_, err := session.ChannelMessageSendEmbed(m.ChannelID, embed) Context: ctx,
if err != nil { Message: "You don't have the correct permissions to run this command.",
log.Println(err) Error: errors.New("insufficient permissions"),
}
} }
return return
} }
//me, err := session.GuildMember(m.GuildID, session.State.User.ID)
//if err != nil {
// log.Fatal(err)
// return
//}
if !CheckPermissions(session, session.State.User.ID, *channel, command.RequiredPermissions) { if !CheckPermissions(session, session.State.User.ID, *channel, command.RequiredPermissions) {
embed := &discordgo.MessageEmbed{ ctx := Context{
Title: "Insufficient Permissions", Session: session,
Description: "I don't have the correct permissions to run this command.", Channel: channel,
Color: 0xFF0000, Message: m.Message,
User: m.Author,
Guild: guild,
Member: m.Member,
Invoked: cmd[0],
ErrorChannel: c.ErrorChannel,
} }
if !command.Hidden { c.ErrorChannel <- CommandError{
_, err := session.ChannelMessageSendEmbed(m.ChannelID, embed) Context: ctx,
if err != nil { Message: "I don't have the correct permissions to run this command.",
log.Println(err) Error: errors.New("insufficient permissions"),
}
} }
return return
} }
if command.OwnerOnly && !c.IsOwner(m.Author.ID) { if command.OwnerOnly && !c.IsOwner(m.Author.ID) {
embed := &discordgo.MessageEmbed{ ctx := Context{
Title: "You can't run that command!", Session: session,
Description: "Sorry, only the bot owner(s) can run that command!", Channel: channel,
Color: 0xff0000, Message: m.Message,
User: m.Author,
Guild: guild,
Member: m.Member,
Invoked: cmd[0],
ErrorChannel: c.ErrorChannel,
} }
c.ErrorChannel <- CommandError{
if !command.Hidden { Context: ctx,
_, err := session.ChannelMessageSendEmbed(m.ChannelID, embed) Message: "Sorry, only the bot owner(s) can run that command!",
if err != nil { Error: errors.New("insufficient permissions"),
log.Println(err)
}
} }
return return
}
guild, _ := session.Guild(m.GuildID) }
context := Context{ context := Context{
Session: session, Session: session,

Loading…
Cancel
Save