2020-04-11 14:16:09 +00:00
|
|
|
package features
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/bwmarrin/discord.go"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-05-04 22:03:11 +00:00
|
|
|
type commandHandler func(session *discordgo.Session, m *discordgo.MessageCreate) error
|
2020-04-11 14:16:09 +00:00
|
|
|
|
2020-05-04 22:03:11 +00:00
|
|
|
var commandHandlers = make(map[string]commandHandler)
|
|
|
|
|
|
|
|
func (f *Features) RegisterCommand(name string, handlerFunc commandHandler) {
|
|
|
|
log.Printf("[*] Registering Command '%s'\n", name)
|
|
|
|
commandHandlers[name] = handlerFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Features) RegisterHandlers() {
|
|
|
|
// Misc Commands
|
|
|
|
f.RegisterCommand("help", f.handleHelp)
|
|
|
|
f.RegisterCommand("info", f.handleInfo)
|
|
|
|
f.RegisterCommand("md", f.handleMarkdownInfo)
|
|
|
|
f.RegisterCommand("userinfo", f.handleUserInfo)
|
|
|
|
f.RegisterCommand("serverinfo", f.handleServerInfo)
|
|
|
|
f.RegisterCommand("no", f.handleCat)
|
|
|
|
|
|
|
|
// User Settings
|
|
|
|
f.RegisterCommand("colors", f.handleUserColors)
|
|
|
|
f.RegisterCommand("color", f.handleUserColor)
|
|
|
|
|
|
|
|
// Conversion Helpers
|
|
|
|
f.RegisterCommand("ctof", f.handleCtoF)
|
|
|
|
f.RegisterCommand("ftoc", f.handleFtoC)
|
|
|
|
f.RegisterCommand("metofe", f.handleMetersToFeet)
|
|
|
|
f.RegisterCommand("fetome", f.handleFeetToMeters)
|
|
|
|
f.RegisterCommand("cmtoin", f.handleCentimeterToInch)
|
|
|
|
f.RegisterCommand("intocm", f.handleInchToCentimeter)
|
|
|
|
|
|
|
|
// Admin Commands
|
|
|
|
f.RegisterCommand("ping", f.handlePing)
|
2020-05-09 11:56:58 +00:00
|
|
|
f.RegisterCommand("rules", f.handleRules)
|
2020-05-04 22:03:11 +00:00
|
|
|
f.RegisterCommand("status", f.handleSetStatus)
|
|
|
|
f.RegisterCommand("purge", f.handlePurgeChannel)
|
2020-05-08 06:33:08 +00:00
|
|
|
f.RegisterCommand("kick", f.handleKickUser)
|
|
|
|
f.RegisterCommand("ban", f.handleBanUser)
|
2020-05-04 22:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Features) ProcessCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
|
2020-04-27 04:59:08 +00:00
|
|
|
cKey := f.Config.CommandKey
|
2020-05-04 22:03:11 +00:00
|
|
|
cCmd := strings.Split(m.Content, " ")[0]
|
2020-04-27 04:59:08 +00:00
|
|
|
|
2020-04-11 14:16:09 +00:00
|
|
|
// Ignore the bot itself
|
|
|
|
if m.Author.ID == s.State.User.ID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-04 22:03:11 +00:00
|
|
|
// Ignore anything not starting with the command prefix
|
|
|
|
if !strings.HasPrefix(cCmd, cKey) {
|
2020-04-27 04:59:08 +00:00
|
|
|
return
|
|
|
|
}
|
2020-04-11 14:16:09 +00:00
|
|
|
|
2020-05-04 22:03:11 +00:00
|
|
|
// Ignore Direct Messages
|
|
|
|
if m.Member == nil {
|
2020-04-11 14:16:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-04 22:03:11 +00:00
|
|
|
cName := strings.Split(cCmd, cKey)[1]
|
2020-04-27 04:59:08 +00:00
|
|
|
|
2020-05-04 22:03:11 +00:00
|
|
|
if cmdFunc, ok := commandHandlers[cName]; ok {
|
|
|
|
log.Printf("[*] Running command %s (Requested by %s)\n", cName, m.Author.Username)
|
|
|
|
|
|
|
|
err := cmdFunc(s, m)
|
2020-04-11 14:16:09 +00:00
|
|
|
if err != nil {
|
2020-05-04 22:03:11 +00:00
|
|
|
log.Printf("[*] Command %s (Requested by %s) had error: '%s'\n", cName, m.Author.Username, err.Error())
|
|
|
|
|
|
|
|
eMsg := f.CreateDefinedEmbed("Error ("+cName+")", err.Error(), "error", m.Author)
|
2020-04-11 14:16:09 +00:00
|
|
|
_, err = s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
|
|
|
|
if err != nil {
|
2020-05-04 22:03:11 +00:00
|
|
|
log.Fatal(err)
|
2020-04-11 14:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 22:03:11 +00:00
|
|
|
func (f *Features) ProcessUserJoin(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
|
2020-04-11 14:16:09 +00:00
|
|
|
userChannel, err := s.UserChannelCreate(m.User.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Error (User Join): " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.ChannelMessageSend(userChannel.ID, f.Config.WelcomeText)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Error (User Join): " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, roleID := range f.Config.JoinRoleIDs {
|
|
|
|
err = s.GuildMemberRoleAdd(f.Auth.Guild.ID, m.User.ID, roleID)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Error (User Join)" + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|