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,
if !command.Hidden { Guild: guild,
_, err := session.ChannelMessageSendEmbed(m.ChannelID, embed) Member: m.Member,
if err != nil { Invoked: cmd[0],
log.Println(err) ErrorChannel: c.ErrorChannel,
} }
c.ErrorChannel <- CommandError{
Context: ctx,
Message: "You don't have the correct permissions to run this command.",
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,
if !command.Hidden { Guild: guild,
_, err := session.ChannelMessageSendEmbed(m.ChannelID, embed) Member: m.Member,
if err != nil { Invoked: cmd[0],
log.Println(err) ErrorChannel: c.ErrorChannel,
} }
c.ErrorChannel <- CommandError{
Context: ctx,
Message: "I don't have the correct permissions to run this command.",
Error: errors.New("insufficient permissions"),
} }
return return
}
if command.OwnerOnly && !c.IsOwner(m.Author.ID) {
embed := &discordgo.MessageEmbed{
Title: "You can't run that command!",
Description: "Sorry, only the bot owner(s) can run that command!",
Color: 0xff0000,
} }
if !command.Hidden { if command.OwnerOnly && !c.IsOwner(m.Author.ID) {
_, err := session.ChannelMessageSendEmbed(m.ChannelID, embed) ctx := Context{
if err != nil { Session: session,
log.Println(err) Channel: channel,
Message: m.Message,
User: m.Author,
Guild: guild,
Member: m.Member,
Invoked: cmd[0],
ErrorChannel: c.ErrorChannel,
} }
c.ErrorChannel <- CommandError{
Context: ctx,
Message: "Sorry, only the bot owner(s) can run that command!",
Error: errors.New("insufficient permissions"),
} }
return return
}
guild, _ := session.Guild(m.GuildID) }
context := Context{ context := Context{
Session: session, Session: session,

Loading…
Cancel
Save