master
Marc 2021-05-29 13:17:01 +01:00
parent fd68bc07a1
commit 479f3b5f17
No known key found for this signature in database
GPG Key ID: 0657563F705ACAAE
10 changed files with 385 additions and 385 deletions

View File

@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"flag"
"github.com/foxtrot/scuzzy/features"
"github.com/foxtrot/scuzzy/commands"
"github.com/foxtrot/scuzzy/models"
"github.com/foxtrot/scuzzy/overwatch"
"github.com/foxtrot/scuzzy/permissions"
@ -86,12 +86,12 @@ func main() {
Config.GuildName = g.Name
// Setup Handlers
f := features.Features{
c := commands.Commands{
Token: Token,
Permissions: p,
Config: &Config,
}
f.RegisterHandlers()
c.RegisterHandlers()
// Setup Overwatch
o := overwatch.Overwatch{
@ -100,7 +100,7 @@ func main() {
}
// Add Handlers for Bot
bot.AddHandler(f.ProcessMessage)
bot.AddHandler(c.ProcessMessage)
bot.AddHandler(o.ProcessMessage)
log.Printf("[*] Bot Running.\n")

View File

@ -1,4 +1,4 @@
package features
package commands
import (
"errors"
@ -9,8 +9,8 @@ import (
"time"
)
func (f *Features) handleSetStatus(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handleSetStatus(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use that command.")
}
@ -26,7 +26,7 @@ func (f *Features) handleSetStatus(s *discordgo.Session, m *discordgo.MessageCre
return err
}
msg := f.CreateDefinedEmbed("Set Status", "Operation completed successfully.", "success", m.Author)
msg := c.CreateDefinedEmbed("Set Status", "Operation completed successfully.", "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
@ -35,12 +35,12 @@ func (f *Features) handleSetStatus(s *discordgo.Session, m *discordgo.MessageCre
return nil
}
func (f *Features) handleDisconnect(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handleDisconnect(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("I'm sorry Dave, I'm afraid I can't do that.")
}
msg := f.CreateDefinedEmbed("Disconnect", "Attempting Disconnect...", "", m.Author)
msg := c.CreateDefinedEmbed("Disconnect", "Attempting Disconnect...", "", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
@ -56,8 +56,8 @@ func (f *Features) handleDisconnect(s *discordgo.Session, m *discordgo.MessageCr
return nil
}
func (f *Features) handleReconnect(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handleReconnect(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use that command.")
}
@ -73,7 +73,7 @@ func (f *Features) handleReconnect(s *discordgo.Session, m *discordgo.MessageCre
log.Fatal(err)
}
msg := f.CreateDefinedEmbed("Reconnect", "Reconnected Successfully.\nTime: `"+time.Since(t).String()+"`.", "success", m.Author)
msg := c.CreateDefinedEmbed("Reconnect", "Reconnected Successfully.\nTime: `"+time.Since(t).String()+"`.", "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err

View File

@ -1,4 +1,4 @@
package features
package commands
import (
"errors"
@ -7,15 +7,15 @@ import (
"strings"
)
func (f *Features) handleListCustomRoles(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleListCustomRoles(s *discordgo.Session, m *discordgo.MessageCreate) error {
msgC := "You can choose from the following roles:\n\n"
for _, v := range f.Config.CustomRoles {
for _, v := range c.Config.CustomRoles {
msgC += "<@&" + v.ID + "> (" + v.ShortName + ")\n"
}
msgC += "\n\n Use `" + f.Config.CommandKey + "joinrole <role_name>` to join a role.\n"
msgC += "Example: `" + f.Config.CommandKey + "joinrole pineapple`.\n"
msgC += "\n\n Use `" + c.Config.CommandKey + "joinrole <role_name>` to join a role.\n"
msgC += "Example: `" + c.Config.CommandKey + "joinrole pineapple`.\n"
msg := f.CreateDefinedEmbed("Joinable Roles", msgC, "", m.Author)
msg := c.CreateDefinedEmbed("Joinable Roles", msgC, "", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
@ -25,14 +25,14 @@ func (f *Features) handleListCustomRoles(s *discordgo.Session, m *discordgo.Mess
return nil
}
func (f *Features) handleJoinCustomRole(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleJoinCustomRole(s *discordgo.Session, m *discordgo.MessageCreate) error {
var err error
rUserID := m.Author.ID
userInput := strings.Split(m.Content, " ")
if len(userInput) < 2 {
err = f.handleListCustomRoles(s, m)
err = c.handleListCustomRoles(s, m)
return err
}
@ -40,7 +40,7 @@ func (f *Features) handleJoinCustomRole(s *discordgo.Session, m *discordgo.Messa
desiredRole = strings.ToLower(desiredRole)
desiredRoleID := ""
for _, role := range f.Config.CustomRoles {
for _, role := range c.Config.CustomRoles {
if role.ShortName == desiredRole {
desiredRoleID = role.ID
break
@ -48,7 +48,7 @@ func (f *Features) handleJoinCustomRole(s *discordgo.Session, m *discordgo.Messa
}
if len(desiredRoleID) == 0 {
err = f.handleListCustomRoles(s, m)
err = c.handleListCustomRoles(s, m)
return err
}
@ -56,7 +56,7 @@ func (f *Features) handleJoinCustomRole(s *discordgo.Session, m *discordgo.Messa
if err != nil {
return err
} else {
msg := f.CreateDefinedEmbed("Join Role", "<@"+m.Author.ID+">: You have joined <@&"+desiredRoleID+">!", "success", m.Author)
msg := c.CreateDefinedEmbed("Join Role", "<@"+m.Author.ID+">: You have joined <@&"+desiredRoleID+">!", "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
@ -71,14 +71,14 @@ func (f *Features) handleJoinCustomRole(s *discordgo.Session, m *discordgo.Messa
return nil
}
func (f *Features) handleLeaveCustomRole(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleLeaveCustomRole(s *discordgo.Session, m *discordgo.MessageCreate) error {
var err error
rUserID := m.Author.ID
userInput := strings.Split(m.Content, " ")
if len(userInput) < 2 {
err = f.handleListCustomRoles(s, m)
err = c.handleListCustomRoles(s, m)
return err
}
@ -86,7 +86,7 @@ func (f *Features) handleLeaveCustomRole(s *discordgo.Session, m *discordgo.Mess
desiredRole = strings.ToLower(desiredRole)
desiredRoleID := ""
for _, role := range f.Config.CustomRoles {
for _, role := range c.Config.CustomRoles {
if role.ShortName == desiredRole {
desiredRoleID = role.ID
break
@ -94,7 +94,7 @@ func (f *Features) handleLeaveCustomRole(s *discordgo.Session, m *discordgo.Mess
}
if len(desiredRoleID) == 0 {
err = f.handleListCustomRoles(s, m)
err = c.handleListCustomRoles(s, m)
return err
}
@ -102,7 +102,7 @@ func (f *Features) handleLeaveCustomRole(s *discordgo.Session, m *discordgo.Mess
if err != nil {
return err
} else {
msg := f.CreateDefinedEmbed("Leave Role", "<@"+m.Author.ID+">: You have left <@&"+desiredRoleID+">!", "success", m.Author)
msg := c.CreateDefinedEmbed("Leave Role", "<@"+m.Author.ID+">: You have left <@&"+desiredRoleID+">!", "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
@ -117,10 +117,10 @@ func (f *Features) handleLeaveCustomRole(s *discordgo.Session, m *discordgo.Mess
return nil
}
func (f *Features) handleAddCustomRole(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleAddCustomRole(s *discordgo.Session, m *discordgo.MessageCreate) error {
var err error
if !f.Permissions.CheckAdminRole(m.Member) {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use that command.")
}
@ -139,9 +139,9 @@ func (f *Features) handleAddCustomRole(s *discordgo.Session, m *discordgo.Messag
ID: roleID,
}
f.Config.CustomRoles = append(f.Config.CustomRoles, customRole)
c.Config.CustomRoles = append(c.Config.CustomRoles, customRole)
err = f.handleSaveConfig(s, m)
err = c.handleSaveConfig(s, m)
if err != nil {
return err
}

View File

@ -1,4 +1,4 @@
package features
package commands
import (
"github.com/bwmarrin/discordgo"
@ -16,7 +16,7 @@ type ScuzzyCommand struct {
Handler ScuzzyHandler
}
type Features struct {
type Commands struct {
Token string
Permissions *permissions.Permissions
Config *models.Configuration

226
commands/handlers.go Normal file
View File

@ -0,0 +1,226 @@
package commands
import (
"errors"
"log"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
)
func (c *Commands) RegisterCommand(name string, description string, adminonly bool, handler ScuzzyHandler) {
log.Printf("[*] Registering Command '%s'\n", name)
co := ScuzzyCommand{
Index: len(c.ScuzzyCommands) + 1,
Name: name,
Description: description,
AdminOnly: adminonly,
Handler: handler,
}
c.ScuzzyCommands[name] = co
c.ScuzzyCommandsByIndex[co.Index] = co
}
func (c *Commands) RegisterHandlers() {
c.ScuzzyCommands = make(map[string]ScuzzyCommand)
c.ScuzzyCommandsByIndex = make(map[int]ScuzzyCommand)
// Misc Commands
c.RegisterCommand("help", "Show Help Text", false, c.handleHelp)
c.RegisterCommand("info", "Show Bot Info", false, c.handleInfo)
c.RegisterCommand("md", "Show common Discord MarkDown formatting", false, c.handleMarkdownInfo)
c.RegisterCommand("userinfo", "Display a users information", false, c.handleUserInfo)
c.RegisterCommand("serverinfo", "Display the current servers information", false, c.handleServerInfo)
c.RegisterCommand("no", "", false, c.handleCat)
// User Settings
c.RegisterCommand("colours", "Display available colour roles", false, c.handleUserColors)
c.RegisterCommand("colors", "", false, c.handleUserColors)
c.RegisterCommand("colour", "Set a colour role for yourself", false, c.handleUserColor)
c.RegisterCommand("color", "", false, c.handleUserColor)
c.RegisterCommand("listroles", "List user joinable roles", false, c.handleListCustomRoles)
c.RegisterCommand("joinrole", "Join an available role for yourself", false, c.handleJoinCustomRole)
c.RegisterCommand("leaverole", "Leave an available role", false, c.handleLeaveCustomRole)
// Conversion Helpers
c.RegisterCommand("ctof", "Convert Celsius to Farenheit", false, c.handleCtoF)
c.RegisterCommand("ftoc", "Convert Farenheit to Celsius", false, c.handleFtoC)
c.RegisterCommand("metofe", "Convert Meters to Feet", false, c.handleMetersToFeet)
c.RegisterCommand("fetome", "Convert Feet to Meters", false, c.handleFeetToMeters)
c.RegisterCommand("cmtoin", "Convert Centimeters to Inches", false, c.handleCentimeterToInch)
c.RegisterCommand("intocm", "Convert Inches to Centimeters", false, c.handleInchToCentimeter)
// Admin Commands
c.RegisterCommand("ping", "Ping Scuzzy", true, c.handlePing)
c.RegisterCommand("rules", "Display the Server Rules", true, c.handleRules)
c.RegisterCommand("status", "Set Bot Status", true, c.handleSetStatus)
c.RegisterCommand("purge", "Purge Channel Messages", true, c.handlePurgeChannel)
c.RegisterCommand("kick", "Kick a User", true, c.handleKickUser)
c.RegisterCommand("ban", "Ban a User", true, c.handleBanUser)
c.RegisterCommand("slow", "Set Channel Slow Mode", true, c.handleSetSlowmode)
c.RegisterCommand("unslow", "Unset Channel Slow Mode", true, c.handleUnsetSlowmode)
c.RegisterCommand("ignore", "Add a user to Scuzzy's ignore list", true, c.handleIgnoreUser)
c.RegisterCommand("unignore", "Remove a user from Scuzzy's ignore list", true, c.handleUnIgnoreUser)
c.RegisterCommand("setconfig", "Set Configuration", true, c.handleSetConfig)
c.RegisterCommand("getconfig", "Print Configuration", true, c.handleGetConfig)
c.RegisterCommand("saveconfig", "Save Configuration to Disk", true, c.handleSaveConfig)
c.RegisterCommand("reloadconfig", "Reload Configuration", true, c.handleReloadConfig)
c.RegisterCommand("addrole", "Add a joinable role", true, c.handleAddCustomRole)
}
func (c *Commands) ProcessCommand(s *discordgo.Session, m *discordgo.MessageCreate) error {
cKey := c.Config.CommandKey
cCmd := strings.Split(m.Content, " ")[0]
// Ignore the bot itself
if m.Author.ID == s.State.User.ID {
return nil
}
// Ignore anything not starting with the command prefix
if !strings.HasPrefix(cCmd, cKey) {
return nil
}
// Ignore Direct Messages
if m.Member == nil {
return nil
}
// Ignore any users on the ignore list
if c.Permissions.CheckIgnoredUser(m.Author) {
log.Printf("[*] Ignoring command from ignored user.")
return nil
}
cName := strings.Split(cCmd, cKey)[1]
if cmd, ok := c.ScuzzyCommands[cName]; ok {
if cmd.AdminOnly && !c.Permissions.CheckAdminRole(m.Member) {
log.Printf("[*] User %s tried to run admin command %s\n", m.Author.Username, cName)
return nil
}
log.Printf("[*] Running command %s (Requested by %s)\n", cName, m.Author.Username)
err := cmd.Handler(s, m)
if err != nil {
log.Printf("[!] Command %s (Requested by %s) had error: '%s'\n", cName, m.Author.Username, err.Error())
eMsg := c.CreateDefinedEmbed("Error ("+cName+")", err.Error(), "error", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
if err != nil {
return err
}
}
}
return nil
}
func (c *Commands) ProcessMessageDelete(s *discordgo.Session, m *discordgo.MessageDelete) error {
msgChannelID := m.ChannelID
if m.BeforeDelete == nil {
return errors.New("Couldn't get deleted message data.")
}
msgContent := m.BeforeDelete.Content
msgAuthor := m.BeforeDelete.Author
msg := "`Username` - " + msgAuthor.Username + "#" + msgAuthor.Discriminator + "\n"
msg += "`User ID` - " + msgAuthor.ID + "\n"
msg += "`Channel` - <#" + msgChannelID + ">\n"
msg += "`Message` - " + msgContent + "\n"
embed := c.CreateDefinedEmbed("Deleted Message", msg, "", msgAuthor)
_, err := s.ChannelMessageSendEmbed(c.Config.LoggingChannel, embed)
if err != nil {
return err
}
return nil
}
func (c *Commands) ProcessMessageDeleteBulk(s *discordgo.Session, m *discordgo.MessageDeleteBulk) error {
msgChannelID := m.ChannelID
msg := "`Channel` - <#" + msgChannelID + ">\n"
msg += "Message IDs: \n"
for k, v := range m.Messages {
msg += strconv.Itoa(k) + ": `" + v + "`\n"
}
embed := c.CreateDefinedEmbed("Deleted Bulk Messages", msg, "", nil)
_, err := s.ChannelMessageSendEmbed(c.Config.LoggingChannel, embed)
if err != nil {
return err
}
return nil
}
func (c *Commands) ProcessUserJoin(s *discordgo.Session, m *discordgo.GuildMemberAdd) error {
userChannel, err := s.UserChannelCreate(m.User.ID)
if err != nil {
log.Print("[!] Error (User Join): " + err.Error())
return err
}
_, err = s.ChannelMessageSend(userChannel.ID, c.Config.WelcomeText)
if err != nil {
log.Print("[!] Error (User Join): " + err.Error())
return err
}
for _, roleID := range c.Config.JoinRoleIDs {
err = s.GuildMemberRoleAdd(c.Config.GuildID, m.User.ID, roleID)
if err != nil {
log.Print("[!] Error (User Join)" + err.Error())
return err
}
}
return nil
}
func (c *Commands) ProcessMessage(s *discordgo.Session, m interface{}) {
switch m.(type) {
case *discordgo.MessageCreate:
// Pass Messages to the command processor
err := c.ProcessCommand(s, m.(*discordgo.MessageCreate))
if err != nil {
log.Println("[!] Error: " + err.Error())
}
break
case *discordgo.MessageDelete:
// Log deleted messages to the logging channel.
err := c.ProcessMessageDelete(s, m.(*discordgo.MessageDelete))
if err != nil {
eMsg := c.CreateDefinedEmbed("Error (Message Deleted)", err.Error(), "error", nil)
_, err = s.ChannelMessageSendEmbed(c.Config.LoggingChannel, eMsg)
if err != nil {
log.Println("[!] Error " + err.Error())
}
}
break
case *discordgo.MessageDeleteBulk:
err := c.ProcessMessageDeleteBulk(s, m.(*discordgo.MessageDeleteBulk))
if err != nil {
eMsg := c.CreateDefinedEmbed("Error (Message Bulk Deleted)", err.Error(), "error", nil)
_, err = s.ChannelMessageSendEmbed(c.Config.LoggingChannel, eMsg)
if err != nil {
log.Println("[!] Error " + err.Error())
}
}
break
case *discordgo.GuildMemberAdd:
// Handle new member (Welcome message, etc)
err := c.ProcessUserJoin(s, m.(*discordgo.GuildMemberAdd))
if err != nil {
log.Println("[!] Error (Guild Member Joined): " + err.Error())
}
break
}
}

View File

@ -1,4 +1,4 @@
package features
package commands
import (
"github.com/bwmarrin/discordgo"
@ -6,7 +6,7 @@ import (
"time"
)
func (f *Features) CreateDefinedEmbed(title string, desc string, status string, user *discordgo.User) *discordgo.MessageEmbed {
func (c *Commands) CreateDefinedEmbed(title string, desc string, status string, user *discordgo.User) *discordgo.MessageEmbed {
msgColor := 0x000000
switch status {
@ -50,7 +50,7 @@ func (f *Features) CreateDefinedEmbed(title string, desc string, status string,
return &msg
}
func (f *Features) CreateCustomEmbed(embedData *models.CustomEmbed) *discordgo.MessageEmbed {
func (c *Commands) CreateCustomEmbed(embedData *models.CustomEmbed) *discordgo.MessageEmbed {
var typ discordgo.EmbedType
var ftr discordgo.MessageEmbedFooter
var img discordgo.MessageEmbedImage

View File

@ -1,4 +1,4 @@
package features
package commands
import (
"encoding/json"
@ -16,28 +16,28 @@ import (
"github.com/foxtrot/scuzzy/models"
)
func (f *Features) handleSetConfig(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handleSetConfig(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use that command.")
}
configArgs := strings.Split(m.Content, " ")
if len(configArgs) != 3 {
return errors.New("Invalid arguments supplied. Usage: " + f.Config.CommandKey + "setconfig <key> <value>")
return errors.New("Invalid arguments supplied. Usage: " + c.Config.CommandKey + "setconfig <key> <value>")
}
configKey := configArgs[1]
configVal := configArgs[2]
rt := reflect.TypeOf(f.Config)
rt := reflect.TypeOf(c.Config)
for i := 0; i < rt.NumField(); i++ {
x := rt.Field(i)
tagVal := strings.Split(x.Tag.Get("json"), ",")[0]
tagName := x.Name
if tagVal == configKey {
prop := reflect.ValueOf(&f.Config).Elem().FieldByName(tagName)
prop := reflect.ValueOf(&c.Config).Elem().FieldByName(tagName)
switch prop.Interface().(type) {
case string:
@ -68,7 +68,7 @@ func (f *Features) handleSetConfig(s *discordgo.Session, m *discordgo.MessageCre
return errors.New("Unsupported key value type")
}
msgE := f.CreateDefinedEmbed("Set Configuration", "Successfully set property '"+configKey+"'!", "success", m.Author)
msgE := c.CreateDefinedEmbed("Set Configuration", "Successfully set property '"+configKey+"'!", "success", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, msgE)
if err != nil {
return err
@ -81,10 +81,10 @@ func (f *Features) handleSetConfig(s *discordgo.Session, m *discordgo.MessageCre
return errors.New("Unknown key specified")
}
func (f *Features) handleGetConfig(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleGetConfig(s *discordgo.Session, m *discordgo.MessageCreate) error {
//TODO: Handle printing of slices (check the Type, loop accordingly)
if !f.Permissions.CheckAdminRole(m.Member) {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use that command.")
}
@ -96,12 +96,12 @@ func (f *Features) handleGetConfig(s *discordgo.Session, m *discordgo.MessageCre
msg := ""
rt := reflect.TypeOf(f.Config)
rt := reflect.TypeOf(c.Config)
for i := 0; i < rt.NumField(); i++ {
x := rt.Field(i)
tagVal := strings.Split(x.Tag.Get("json"), ",")[0]
tagName := x.Name
prop := reflect.ValueOf(&f.Config).Elem().FieldByName(tagName)
prop := reflect.ValueOf(&c.Config).Elem().FieldByName(tagName)
if configKey == "all" {
switch prop.Interface().(type) {
@ -128,7 +128,7 @@ func (f *Features) handleGetConfig(s *discordgo.Session, m *discordgo.MessageCre
msg += "`" + tagName + "` - Skipped Value\n"
}
eMsg := f.CreateDefinedEmbed("Get Configuration", msg, "success", m.Author)
eMsg := c.CreateDefinedEmbed("Get Configuration", msg, "success", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
if err != nil {
return err
@ -143,7 +143,7 @@ func (f *Features) handleGetConfig(s *discordgo.Session, m *discordgo.MessageCre
return errors.New("Unknown key specified")
}
eMsg := f.CreateDefinedEmbed("Get Configuration", msg, "success", m.Author)
eMsg := c.CreateDefinedEmbed("Get Configuration", msg, "success", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
if err != nil {
return err
@ -152,12 +152,12 @@ func (f *Features) handleGetConfig(s *discordgo.Session, m *discordgo.MessageCre
return nil
}
func (f *Features) handleReloadConfig(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handleReloadConfig(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use this command.")
}
fBuf, err := ioutil.ReadFile(f.Config.ConfigPath)
fBuf, err := ioutil.ReadFile(c.Config.ConfigPath)
if err != nil {
return err
}
@ -169,10 +169,10 @@ func (f *Features) handleReloadConfig(s *discordgo.Session, m *discordgo.Message
return err
}
f.Config = conf
f.Permissions.Config = conf
c.Config = conf
c.Permissions.Config = conf
eMsg := f.CreateDefinedEmbed("Reload Configuration", "Successfully reloaded configuration from disk", "success", m.Author)
eMsg := c.CreateDefinedEmbed("Reload Configuration", "Successfully reloaded configuration from disk", "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
if err != nil {
return err
@ -181,22 +181,22 @@ func (f *Features) handleReloadConfig(s *discordgo.Session, m *discordgo.Message
return nil
}
func (f *Features) handleSaveConfig(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handleSaveConfig(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use this command.")
}
j, err := json.Marshal(f.Config)
j, err := json.Marshal(c.Config)
if err != nil {
return err
}
err = ioutil.WriteFile(f.Config.ConfigPath, j, os.ModePerm)
err = ioutil.WriteFile(c.Config.ConfigPath, j, os.ModePerm)
if err != nil {
return err
}
eMsg := f.CreateDefinedEmbed("Save Configuration", "Saved runtime configuration successfully", "success", m.Author)
eMsg := c.CreateDefinedEmbed("Save Configuration", "Saved runtime configuration successfully", "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
if err != nil {
return err
@ -205,7 +205,7 @@ func (f *Features) handleSaveConfig(s *discordgo.Session, m *discordgo.MessageCr
return nil
}
func (f *Features) handleCat(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleCat(s *discordgo.Session, m *discordgo.MessageCreate) error {
_, err := s.ChannelMessageSend(m.ChannelID, "https://giphy.com/gifs/cat-cute-no-rCxogJBzaeZuU")
if err != nil {
return err
@ -219,14 +219,14 @@ func (f *Features) handleCat(s *discordgo.Session, m *discordgo.MessageCreate) e
return nil
}
func (f *Features) handlePing(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handlePing(s *discordgo.Session, m *discordgo.MessageCreate) error {
var r *discordgo.Message
var err error
if !f.Permissions.CheckAdminRole(m.Member) {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use that command.")
} else {
msg := f.CreateDefinedEmbed("Ping", "Pong", "success", m.Author)
msg := c.CreateDefinedEmbed("Ping", "Pong", "success", m.Author)
r, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
@ -247,12 +247,12 @@ func (f *Features) handlePing(s *discordgo.Session, m *discordgo.MessageCreate)
return nil
}
func (f *Features) handleInfo(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleInfo(s *discordgo.Session, m *discordgo.MessageCreate) error {
desc := "**Source**: https://github.com/foxtrot/scuzzy\n"
desc += "**Language**: Go\n"
desc += "**Commands**: See `" + f.Config.CommandKey + "help`\n\n\n"
desc += "**Commands**: See `" + c.Config.CommandKey + "help`\n\n\n"
gm, err := s.GuildMember(f.Config.GuildID, s.State.User.ID)
gm, err := s.GuildMember(c.Config.GuildID, s.State.User.ID)
if err != nil {
return err
}
@ -279,7 +279,7 @@ func (f *Features) handleInfo(s *discordgo.Session, m *discordgo.MessageCreate)
AuthorImageURL: "",
}
msg := f.CreateCustomEmbed(&d)
msg := c.CreateCustomEmbed(&d)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
@ -289,31 +289,31 @@ func (f *Features) handleInfo(s *discordgo.Session, m *discordgo.MessageCreate)
return nil
}
func (f *Features) handleHelp(s *discordgo.Session, m *discordgo.MessageCreate) error {
keys := make([]int, 0, len(f.ScuzzyCommands))
for _, cmd := range f.ScuzzyCommands {
func (c *Commands) handleHelp(s *discordgo.Session, m *discordgo.MessageCreate) error {
keys := make([]int, 0, len(c.ScuzzyCommands))
for _, cmd := range c.ScuzzyCommands {
keys = append(keys, cmd.Index)
}
sort.Ints(keys)
for _, k := range keys {
fmt.Println(k, f.ScuzzyCommandsByIndex[k])
fmt.Println(k, c.ScuzzyCommandsByIndex[k])
}
desc := "**Available Commands**\n"
for _, k := range keys {
command := f.ScuzzyCommandsByIndex[k]
command := c.ScuzzyCommandsByIndex[k]
if !command.AdminOnly && command.Description != "" {
desc += "`" + command.Name + "` - " + command.Description + "\n"
}
}
if f.Permissions.CheckAdminRole(m.Member) {
if c.Permissions.CheckAdminRole(m.Member) {
desc += "\n"
desc += "**Admin Commands**\n"
for _, k := range keys {
command := f.ScuzzyCommandsByIndex[k]
command := c.ScuzzyCommandsByIndex[k]
if command.AdminOnly {
desc += "`" + command.Name + "` - " + command.Description + "\n"
@ -321,9 +321,9 @@ func (f *Features) handleHelp(s *discordgo.Session, m *discordgo.MessageCreate)
}
}
desc += "\n\nAll commands are prefixed with `" + f.Config.CommandKey + "`\n"
desc += "\n\nAll commands are prefixed with `" + c.Config.CommandKey + "`\n"
msg := f.CreateDefinedEmbed("Help", desc, "", m.Author)
msg := c.CreateDefinedEmbed("Help", desc, "", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
@ -337,14 +337,14 @@ func (f *Features) handleHelp(s *discordgo.Session, m *discordgo.MessageCreate)
return nil
}
func (f *Features) handleRules(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handleRules(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use that command.")
}
msg := f.Config.RulesText
embedTitle := "Rules (" + f.Config.GuildName + ")"
embed := f.CreateDefinedEmbed(embedTitle, msg, "success", m.Author)
msg := c.Config.RulesText
embedTitle := "Rules (" + c.Config.GuildName + ")"
embed := c.CreateDefinedEmbed(embedTitle, msg, "success", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, embed)
if err != nil {
@ -354,12 +354,12 @@ func (f *Features) handleRules(s *discordgo.Session, m *discordgo.MessageCreate)
return nil
}
func (f *Features) handleMarkdownInfo(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleMarkdownInfo(s *discordgo.Session, m *discordgo.MessageCreate) error {
cleanup := true
args := strings.Split(m.Content, " ")
if len(args) == 2 {
if args[1] == "stay" && f.Permissions.CheckAdminRole(m.Member) {
if args[1] == "stay" && c.Permissions.CheckAdminRole(m.Member) {
cleanup = false
}
}
@ -377,7 +377,7 @@ func (f *Features) handleMarkdownInfo(s *discordgo.Session, m *discordgo.Message
desc += "Single line quotes start with `>`\n"
desc += "Multi line quotes start with `>>>`\n"
msg := f.CreateDefinedEmbed("Discord Markdown", desc, "", m.Author)
msg := c.CreateDefinedEmbed("Discord Markdown", desc, "", m.Author)
r, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
@ -399,7 +399,7 @@ func (f *Features) handleMarkdownInfo(s *discordgo.Session, m *discordgo.Message
return nil
}
func (f *Features) handleCtoF(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleCtoF(s *discordgo.Session, m *discordgo.MessageCreate) error {
inS := strings.Split(m.Content, " ")
if len(inS) < 2 {
@ -417,7 +417,7 @@ func (f *Features) handleCtoF(s *discordgo.Session, m *discordgo.MessageCreate)
msg := fmt.Sprintf("`%.1f°c` is `%.1f°f`", inF, celsF)
e := f.CreateDefinedEmbed("Celsius to Farenheit", msg, "", m.Author)
e := c.CreateDefinedEmbed("Celsius to Farenheit", msg, "", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, e)
if err != nil {
return err
@ -426,7 +426,7 @@ func (f *Features) handleCtoF(s *discordgo.Session, m *discordgo.MessageCreate)
return nil
}
func (f *Features) handleFtoC(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleFtoC(s *discordgo.Session, m *discordgo.MessageCreate) error {
inS := strings.Split(m.Content, " ")
if len(inS) < 2 {
@ -444,7 +444,7 @@ func (f *Features) handleFtoC(s *discordgo.Session, m *discordgo.MessageCreate)
msg := fmt.Sprintf("`%.1f°f` is `%.1f°c`", inF, farenF)
e := f.CreateDefinedEmbed("Farenheit to Celsius", msg, "", m.Author)
e := c.CreateDefinedEmbed("Farenheit to Celsius", msg, "", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, e)
if err != nil {
return err
@ -453,7 +453,7 @@ func (f *Features) handleFtoC(s *discordgo.Session, m *discordgo.MessageCreate)
return nil
}
func (f *Features) handleMetersToFeet(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleMetersToFeet(s *discordgo.Session, m *discordgo.MessageCreate) error {
inS := strings.Split(m.Content, " ")
if len(inS) < 2 {
@ -471,7 +471,7 @@ func (f *Features) handleMetersToFeet(s *discordgo.Session, m *discordgo.Message
msg := fmt.Sprintf("`%.1fm` is `%.1fft`", inF, metersF)
e := f.CreateDefinedEmbed("Meters to Feet", msg, "", m.Author)
e := c.CreateDefinedEmbed("Meters to Feet", msg, "", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, e)
if err != nil {
return err
@ -480,7 +480,7 @@ func (f *Features) handleMetersToFeet(s *discordgo.Session, m *discordgo.Message
return nil
}
func (f *Features) handleFeetToMeters(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleFeetToMeters(s *discordgo.Session, m *discordgo.MessageCreate) error {
inS := strings.Split(m.Content, " ")
if len(inS) < 2 {
@ -498,7 +498,7 @@ func (f *Features) handleFeetToMeters(s *discordgo.Session, m *discordgo.Message
msg := fmt.Sprintf("`%.1fft` is `%.1fm`", inF, feetF)
e := f.CreateDefinedEmbed("Feet to Meters", msg, "", m.Author)
e := c.CreateDefinedEmbed("Feet to Meters", msg, "", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, e)
if err != nil {
return err
@ -507,7 +507,7 @@ func (f *Features) handleFeetToMeters(s *discordgo.Session, m *discordgo.Message
return nil
}
func (f *Features) handleCentimeterToInch(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleCentimeterToInch(s *discordgo.Session, m *discordgo.MessageCreate) error {
inS := strings.Split(m.Content, " ")
if len(inS) < 2 {
@ -525,7 +525,7 @@ func (f *Features) handleCentimeterToInch(s *discordgo.Session, m *discordgo.Mes
msg := fmt.Sprintf("`%.1fcm` is `%.1fin`", inF, inchF)
e := f.CreateDefinedEmbed("Centimeter To Inch", msg, "", m.Author)
e := c.CreateDefinedEmbed("Centimeter To Inch", msg, "", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, e)
if err != nil {
return err
@ -534,7 +534,7 @@ func (f *Features) handleCentimeterToInch(s *discordgo.Session, m *discordgo.Mes
return nil
}
func (f *Features) handleInchToCentimeter(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleInchToCentimeter(s *discordgo.Session, m *discordgo.MessageCreate) error {
inS := strings.Split(m.Content, " ")
if len(inS) < 2 {
@ -552,7 +552,7 @@ func (f *Features) handleInchToCentimeter(s *discordgo.Session, m *discordgo.Mes
msg := fmt.Sprintf("`%.1fin` is `%.1fcm`", inF, cmF)
e := f.CreateDefinedEmbed("Inch to Centimeter", msg, "", m.Author)
e := c.CreateDefinedEmbed("Inch to Centimeter", msg, "", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, e)
if err != nil {
return err
@ -561,7 +561,7 @@ func (f *Features) handleInchToCentimeter(s *discordgo.Session, m *discordgo.Mes
return nil
}
func (f *Features) handleUserInfo(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleUserInfo(s *discordgo.Session, m *discordgo.MessageCreate) error {
var (
mHandle *discordgo.Member
requester *discordgo.Member
@ -571,7 +571,7 @@ func (f *Features) handleUserInfo(s *discordgo.Session, m *discordgo.MessageCrea
userSplit := strings.Split(m.Content, " ")
if len(userSplit) < 2 {
mHandle, err = s.GuildMember(f.Config.GuildID, m.Author.ID)
mHandle, err = s.GuildMember(c.Config.GuildID, m.Author.ID)
requester = mHandle
if err != nil {
return err
@ -580,11 +580,11 @@ func (f *Features) handleUserInfo(s *discordgo.Session, m *discordgo.MessageCrea
idStr := strings.ReplaceAll(userSplit[1], "<@!", "")
idStr = strings.ReplaceAll(idStr, "<@", "")
idStr = strings.ReplaceAll(idStr, ">", "")
mHandle, err = s.GuildMember(f.Config.GuildID, idStr)
mHandle, err = s.GuildMember(c.Config.GuildID, idStr)
if err != nil {
return err
}
requester, err = s.GuildMember(f.Config.GuildID, m.Author.ID)
requester, err = s.GuildMember(c.Config.GuildID, m.Author.ID)
if err != nil {
return err
}
@ -645,7 +645,7 @@ func (f *Features) handleUserInfo(s *discordgo.Session, m *discordgo.MessageCrea
AuthorImageURL: "",
}
embed := f.CreateCustomEmbed(&embedData)
embed := c.CreateCustomEmbed(&embedData)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, embed)
if err != nil {
return err
@ -654,22 +654,22 @@ func (f *Features) handleUserInfo(s *discordgo.Session, m *discordgo.MessageCrea
return nil
}
func (f *Features) handleServerInfo(s *discordgo.Session, m *discordgo.MessageCreate) error {
g, err := s.Guild(f.Config.GuildID)
func (c *Commands) handleServerInfo(s *discordgo.Session, m *discordgo.MessageCreate) error {
g, err := s.Guild(c.Config.GuildID)
if err != nil {
return err
}
sID := f.Config.GuildID
sName := f.Config.GuildName
sID := c.Config.GuildID
sName := c.Config.GuildName
chans, _ := s.GuildChannels(f.Config.GuildID)
chans, _ := s.GuildChannels(c.Config.GuildID)
sChannels := strconv.Itoa(len(chans))
sEmojis := strconv.Itoa(len(g.Emojis))
sRoles := strconv.Itoa(len(g.Roles))
sRegion := g.Region
iID, _ := strconv.Atoi(f.Config.GuildID)
iID, _ := strconv.Atoi(c.Config.GuildID)
createdMSecs := ((iID / 4194304) + 1420070400000) / 1000
sCreatedAt := time.Unix(int64(createdMSecs), 0).Format(time.RFC1123)
@ -707,7 +707,7 @@ func (f *Features) handleServerInfo(s *discordgo.Session, m *discordgo.MessageCr
AuthorImageURL: "",
}
msg := f.CreateCustomEmbed(&embedData)
msg := c.CreateCustomEmbed(&embedData)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {

View File

@ -1,4 +1,4 @@
package features
package commands
import (
"errors"
@ -8,8 +8,8 @@ import (
"time"
)
func (f *Features) handleSetSlowmode(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handleSetSlowmode(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use that command.")
}
@ -26,7 +26,7 @@ func (f *Features) handleSetSlowmode(s *discordgo.Session, m *discordgo.MessageC
if len(slowmodeSplit) == 3 {
if slowmodeSplit[2] == "all" {
channels, err := s.GuildChannels(f.Config.GuildID)
channels, err := s.GuildChannels(c.Config.GuildID)
if err != nil {
return err
}
@ -46,7 +46,7 @@ func (f *Features) handleSetSlowmode(s *discordgo.Session, m *discordgo.MessageC
}
}
msg := f.CreateDefinedEmbed("Slow Mode", "Successfully set Slow Mode to `"+slowmodeTimeStr+"`.", "success", m.Author)
msg := c.CreateDefinedEmbed("Slow Mode", "Successfully set Slow Mode to `"+slowmodeTimeStr+"`.", "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
@ -55,8 +55,8 @@ func (f *Features) handleSetSlowmode(s *discordgo.Session, m *discordgo.MessageC
return nil
}
func (f *Features) handleUnsetSlowmode(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handleUnsetSlowmode(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use that command.")
}
@ -64,7 +64,7 @@ func (f *Features) handleUnsetSlowmode(s *discordgo.Session, m *discordgo.Messag
if len(slowmodeSplit) == 2 {
if slowmodeSplit[1] == "all" {
channels, err := s.GuildChannels(f.Config.GuildID)
channels, err := s.GuildChannels(c.Config.GuildID)
if err != nil {
return err
}
@ -84,7 +84,7 @@ func (f *Features) handleUnsetSlowmode(s *discordgo.Session, m *discordgo.Messag
}
}
msg := f.CreateDefinedEmbed("Slow Mode", "Successfully unset Slow Mode", "success", m.Author)
msg := c.CreateDefinedEmbed("Slow Mode", "Successfully unset Slow Mode", "success", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
@ -93,8 +93,8 @@ func (f *Features) handleUnsetSlowmode(s *discordgo.Session, m *discordgo.Messag
return nil
}
func (f *Features) handlePurgeChannel(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handlePurgeChannel(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use that command.")
}
@ -117,7 +117,7 @@ func (f *Features) handlePurgeChannel(s *discordgo.Session, m *discordgo.Message
return err
}
msg := f.CreateDefinedEmbed("Purge Channel", "Purging `"+purgeSplit[1]+"` messages.", "", m.Author)
msg := c.CreateDefinedEmbed("Purge Channel", "Purging `"+purgeSplit[1]+"` messages.", "", m.Author)
r, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
@ -134,7 +134,7 @@ func (f *Features) handlePurgeChannel(s *discordgo.Session, m *discordgo.Message
}
err = s.ChannelMessageDelete(m.ChannelID, r.ID)
msg = f.CreateDefinedEmbed("Purge Channel", "Purged `"+purgeSplit[1]+"` messages!", "success", m.Author)
msg = c.CreateDefinedEmbed("Purge Channel", "Purged `"+purgeSplit[1]+"` messages!", "success", m.Author)
msgS, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
@ -150,7 +150,7 @@ func (f *Features) handlePurgeChannel(s *discordgo.Session, m *discordgo.Message
return nil
}
func (f *Features) handleKickUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleKickUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
var (
mHandle *discordgo.Member
kickReason string
@ -169,12 +169,12 @@ func (f *Features) handleKickUser(s *discordgo.Session, m *discordgo.MessageCrea
idStr := strings.ReplaceAll(member, "<@!", "")
idStr = strings.ReplaceAll(idStr, "<@", "")
idStr = strings.ReplaceAll(idStr, ">", "")
mHandle, err = s.GuildMember(f.Config.GuildID, idStr)
mHandle, err = s.GuildMember(c.Config.GuildID, idStr)
if err != nil {
return err
}
err = s.GuildMemberDeleteWithReason(f.Config.GuildID, mHandle.User.ID, kickReason)
err = s.GuildMemberDeleteWithReason(c.Config.GuildID, mHandle.User.ID, kickReason)
if err != nil {
return err
}
@ -184,7 +184,7 @@ func (f *Features) handleKickUser(s *discordgo.Session, m *discordgo.MessageCrea
msg += "Reason: `" + kickReason + "`\n"
}
embed := f.CreateDefinedEmbed("Kick User", msg, "success", m.Author)
embed := c.CreateDefinedEmbed("Kick User", msg, "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, embed)
if err != nil {
return err
@ -193,8 +193,8 @@ func (f *Features) handleKickUser(s *discordgo.Session, m *discordgo.MessageCrea
return nil
}
func (f *Features) handleBanUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handleBanUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use this command.")
}
@ -221,7 +221,7 @@ func (f *Features) handleBanUser(s *discordgo.Session, m *discordgo.MessageCreat
return err
}
err = s.GuildBanCreateWithReason(f.Config.GuildID, mHandle.ID, banReason, 0)
err = s.GuildBanCreateWithReason(c.Config.GuildID, mHandle.ID, banReason, 0)
if err != nil {
return err
}
@ -231,7 +231,7 @@ func (f *Features) handleBanUser(s *discordgo.Session, m *discordgo.MessageCreat
msg += "Reason: `" + banReason + "`\n"
}
embed := f.CreateDefinedEmbed("Ban User", msg, "success", m.Author)
embed := c.CreateDefinedEmbed("Ban User", msg, "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, embed)
if err != nil {
return err
@ -240,8 +240,8 @@ func (f *Features) handleBanUser(s *discordgo.Session, m *discordgo.MessageCreat
return nil
}
func (f *Features) handleIgnoreUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handleIgnoreUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use this command.")
}
@ -255,15 +255,15 @@ func (f *Features) handleIgnoreUser(s *discordgo.Session, m *discordgo.MessageCr
idStr = strings.ReplaceAll(idStr, "<@", "")
idStr = strings.ReplaceAll(idStr, ">", "")
f.Config.IgnoredUsers = append(f.Config.IgnoredUsers, idStr)
c.Config.IgnoredUsers = append(c.Config.IgnoredUsers, idStr)
eMsg := f.CreateDefinedEmbed("Ignore User", "<@!"+idStr+"> is now being ignored.", "success", m.Author)
eMsg := c.CreateDefinedEmbed("Ignore User", "<@!"+idStr+"> is now being ignored.", "success", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
if err != nil {
return err
}
err = f.handleSaveConfig(s, m)
err = c.handleSaveConfig(s, m)
if err != nil {
return err
}
@ -271,8 +271,8 @@ func (f *Features) handleIgnoreUser(s *discordgo.Session, m *discordgo.MessageCr
return nil
}
func (f *Features) handleUnIgnoreUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckAdminRole(m.Member) {
func (c *Commands) handleUnIgnoreUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckAdminRole(m.Member) {
return errors.New("You do not have permissions to use this command.")
}
@ -286,20 +286,20 @@ func (f *Features) handleUnIgnoreUser(s *discordgo.Session, m *discordgo.Message
idStr = strings.ReplaceAll(idStr, "<@", "")
idStr = strings.ReplaceAll(idStr, ">", "")
for k, v := range f.Config.IgnoredUsers {
for k, v := range c.Config.IgnoredUsers {
if v == idStr {
f.Config.IgnoredUsers[k] = f.Config.IgnoredUsers[len(f.Config.IgnoredUsers)-1]
f.Config.IgnoredUsers = f.Config.IgnoredUsers[:len(f.Config.IgnoredUsers)-1]
c.Config.IgnoredUsers[k] = c.Config.IgnoredUsers[len(c.Config.IgnoredUsers)-1]
c.Config.IgnoredUsers = c.Config.IgnoredUsers[:len(c.Config.IgnoredUsers)-1]
}
}
eMsg := f.CreateDefinedEmbed("Unignore User", "<@!"+idStr+"> is not being ignored.", "success", m.Author)
eMsg := c.CreateDefinedEmbed("Unignore User", "<@!"+idStr+"> is not being ignored.", "success", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
if err != nil {
return err
}
err = f.handleSaveConfig(s, m)
err = c.handleSaveConfig(s, m)
if err != nil {
return err
}

View File

@ -1,4 +1,4 @@
package features
package commands
import (
"errors"
@ -6,19 +6,19 @@ import (
"strings"
)
func (f *Features) handleUserColors(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !f.Permissions.CheckCommandRestrictions(m) {
func (c *Commands) handleUserColors(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !c.Permissions.CheckCommandRestrictions(m) {
return errors.New("This command is not allowed in this channel.")
}
msgC := "You can choose from the following colors:\n\n"
for _, v := range f.Config.ColorRoles {
for _, v := range c.Config.ColorRoles {
msgC += "<@&" + v.ID + ">\n"
}
msgC += "\n\nUse `" + f.Config.CommandKey + "color <color>` to set.\n"
msgC += "Example: `" + f.Config.CommandKey + "color red`.\n"
msgC += "\n\nUse `" + c.Config.CommandKey + "color <color>` to set.\n"
msgC += "Example: `" + c.Config.CommandKey + "color red`.\n"
msg := f.CreateDefinedEmbed("User Colors", msgC, "", m.Author)
msg := c.CreateDefinedEmbed("User Colors", msgC, "", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
@ -28,10 +28,10 @@ func (f *Features) handleUserColors(s *discordgo.Session, m *discordgo.MessageCr
return nil
}
func (f *Features) handleUserColor(s *discordgo.Session, m *discordgo.MessageCreate) error {
func (c *Commands) handleUserColor(s *discordgo.Session, m *discordgo.MessageCreate) error {
var err error
if !f.Permissions.CheckCommandRestrictions(m) {
if !c.Permissions.CheckCommandRestrictions(m) {
return errors.New("This command is not allowed in this channel.")
}
@ -39,25 +39,25 @@ func (f *Features) handleUserColor(s *discordgo.Session, m *discordgo.MessageCre
userInput := strings.Split(m.Content, " ")
if len(userInput) < 2 {
err = f.handleUserColors(s, m)
err = c.handleUserColors(s, m)
return err
}
roleColorName := userInput[1]
roleColorName = strings.ToLower(roleColorName)
roleColorID := ""
for _, role := range f.Config.ColorRoles {
for _, role := range c.Config.ColorRoles {
if role.Name == roleColorName {
roleColorID = role.ID
break
}
}
if len(roleColorID) == 0 {
err = f.handleUserColors(s, m)
err = c.handleUserColors(s, m)
return err
}
for _, role := range f.Config.ColorRoles {
for _, role := range c.Config.ColorRoles {
// Attempt to remove all color roles regardless of if they have them or not.
// Slow because of the REST requests...
_ = s.GuildMemberRoleRemove(m.GuildID, rUserID, role.ID)
@ -67,7 +67,7 @@ func (f *Features) handleUserColor(s *discordgo.Session, m *discordgo.MessageCre
if err != nil {
return err
} else {
msg := f.CreateDefinedEmbed("User Color", "<@"+m.Author.ID+">: Your color has been set to <@&"+roleColorID+">!", "success", m.Author)
msg := c.CreateDefinedEmbed("User Color", "<@"+m.Author.ID+">: Your color has been set to <@&"+roleColorID+">!", "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err

View File

@ -1,226 +0,0 @@
package features
import (
"errors"
"log"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
)
func (f *Features) RegisterCommand(name string, description string, adminonly bool, handler ScuzzyHandler) {
log.Printf("[*] Registering Command '%s'\n", name)
c := ScuzzyCommand{
Index: len(f.ScuzzyCommands) + 1,
Name: name,
Description: description,
AdminOnly: adminonly,
Handler: handler,
}
f.ScuzzyCommands[name] = c
f.ScuzzyCommandsByIndex[c.Index] = c
}
func (f *Features) RegisterHandlers() {
f.ScuzzyCommands = make(map[string]ScuzzyCommand)
f.ScuzzyCommandsByIndex = make(map[int]ScuzzyCommand)
// Misc Commands
f.RegisterCommand("help", "Show Help Text", false, f.handleHelp)
f.RegisterCommand("info", "Show Bot Info", false, f.handleInfo)
f.RegisterCommand("md", "Show common Discord MarkDown formatting", false, f.handleMarkdownInfo)
f.RegisterCommand("userinfo", "Display a users information", false, f.handleUserInfo)
f.RegisterCommand("serverinfo", "Display the current servers information", false, f.handleServerInfo)
f.RegisterCommand("no", "", false, f.handleCat)
// User Settings
f.RegisterCommand("colours", "Display available colour roles", false, f.handleUserColors)
f.RegisterCommand("colors", "", false, f.handleUserColors)
f.RegisterCommand("colour", "Set a colour role for yourself", false, f.handleUserColor)
f.RegisterCommand("color", "", false, f.handleUserColor)
f.RegisterCommand("listroles", "List user joinable roles", false, f.handleListCustomRoles)
f.RegisterCommand("joinrole", "Join an available role for yourself", false, f.handleJoinCustomRole)
f.RegisterCommand("leaverole", "Leave an available role", false, f.handleLeaveCustomRole)
// Conversion Helpers
f.RegisterCommand("ctof", "Convert Celsius to Farenheit", false, f.handleCtoF)
f.RegisterCommand("ftoc", "Convert Farenheit to Celsius", false, f.handleFtoC)
f.RegisterCommand("metofe", "Convert Meters to Feet", false, f.handleMetersToFeet)
f.RegisterCommand("fetome", "Convert Feet to Meters", false, f.handleFeetToMeters)
f.RegisterCommand("cmtoin", "Convert Centimeters to Inches", false, f.handleCentimeterToInch)
f.RegisterCommand("intocm", "Convert Inches to Centimeters", false, f.handleInchToCentimeter)
// Admin Commands
f.RegisterCommand("ping", "Ping Scuzzy", true, f.handlePing)
f.RegisterCommand("rules", "Display the Server Rules", true, f.handleRules)
f.RegisterCommand("status", "Set Bot Status", true, f.handleSetStatus)
f.RegisterCommand("purge", "Purge Channel Messages", true, f.handlePurgeChannel)
f.RegisterCommand("kick", "Kick a User", true, f.handleKickUser)
f.RegisterCommand("ban", "Ban a User", true, f.handleBanUser)
f.RegisterCommand("slow", "Set Channel Slow Mode", true, f.handleSetSlowmode)
f.RegisterCommand("unslow", "Unset Channel Slow Mode", true, f.handleUnsetSlowmode)
f.RegisterCommand("ignore", "Add a user to Scuzzy's ignore list", true, f.handleIgnoreUser)
f.RegisterCommand("unignore", "Remove a user from Scuzzy's ignore list", true, f.handleUnIgnoreUser)
f.RegisterCommand("setconfig", "Set Configuration", true, f.handleSetConfig)
f.RegisterCommand("getconfig", "Print Configuration", true, f.handleGetConfig)
f.RegisterCommand("saveconfig", "Save Configuration to Disk", true, f.handleSaveConfig)
f.RegisterCommand("reloadconfig", "Reload Configuration", true, f.handleReloadConfig)
f.RegisterCommand("addrole", "Add a joinable role", true, f.handleAddCustomRole)
}
func (f *Features) ProcessCommand(s *discordgo.Session, m *discordgo.MessageCreate) error {
cKey := f.Config.CommandKey
cCmd := strings.Split(m.Content, " ")[0]
// Ignore the bot itself
if m.Author.ID == s.State.User.ID {
return nil
}
// Ignore anything not starting with the command prefix
if !strings.HasPrefix(cCmd, cKey) {
return nil
}
// Ignore Direct Messages
if m.Member == nil {
return nil
}
// Ignore any users on the ignore list
if f.Permissions.CheckIgnoredUser(m.Author) {
log.Printf("[*] Ignoring command from ignored user.")
return nil
}
cName := strings.Split(cCmd, cKey)[1]
if cmd, ok := f.ScuzzyCommands[cName]; ok {
if cmd.AdminOnly && !f.Permissions.CheckAdminRole(m.Member) {
log.Printf("[*] User %s tried to run admin command %s\n", m.Author.Username, cName)
return nil
}
log.Printf("[*] Running command %s (Requested by %s)\n", cName, m.Author.Username)
err := cmd.Handler(s, m)
if err != nil {
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)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
if err != nil {
return err
}
}
}
return nil
}
func (f *Features) ProcessMessageDelete(s *discordgo.Session, m *discordgo.MessageDelete) error {
msgChannelID := m.ChannelID
if m.BeforeDelete == nil {
return errors.New("Couldn't get deleted message data.")
}
msgContent := m.BeforeDelete.Content
msgAuthor := m.BeforeDelete.Author
msg := "`Username` - " + msgAuthor.Username + "#" + msgAuthor.Discriminator + "\n"
msg += "`User ID` - " + msgAuthor.ID + "\n"
msg += "`Channel` - <#" + msgChannelID + ">\n"
msg += "`Message` - " + msgContent + "\n"
embed := f.CreateDefinedEmbed("Deleted Message", msg, "", msgAuthor)
_, err := s.ChannelMessageSendEmbed(f.Config.LoggingChannel, embed)
if err != nil {
return err
}
return nil
}
func (f *Features) ProcessMessageDeleteBulk(s *discordgo.Session, m *discordgo.MessageDeleteBulk) error {
msgChannelID := m.ChannelID
msg := "`Channel` - <#" + msgChannelID + ">\n"
msg += "Message IDs: \n"
for k, v := range m.Messages {
msg += strconv.Itoa(k) + ": `" + v + "`\n"
}
embed := f.CreateDefinedEmbed("Deleted Bulk Messages", msg, "", nil)
_, err := s.ChannelMessageSendEmbed(f.Config.LoggingChannel, embed)
if err != nil {
return err
}
return nil
}
func (f *Features) ProcessUserJoin(s *discordgo.Session, m *discordgo.GuildMemberAdd) error {
userChannel, err := s.UserChannelCreate(m.User.ID)
if err != nil {
log.Print("[!] Error (User Join): " + err.Error())
return err
}
_, err = s.ChannelMessageSend(userChannel.ID, f.Config.WelcomeText)
if err != nil {
log.Print("[!] Error (User Join): " + err.Error())
return err
}
for _, roleID := range f.Config.JoinRoleIDs {
err = s.GuildMemberRoleAdd(f.Config.GuildID, m.User.ID, roleID)
if err != nil {
log.Print("[!] Error (User Join)" + err.Error())
return err
}
}
return nil
}
func (f *Features) ProcessMessage(s *discordgo.Session, m interface{}) {
switch m.(type) {
case *discordgo.MessageCreate:
// Pass Messages to the command processor
err := f.ProcessCommand(s, m.(*discordgo.MessageCreate))
if err != nil {
log.Println("[!] Error: " + err.Error())
}
break
case *discordgo.MessageDelete:
// Log deleted messages to the logging channel.
err := f.ProcessMessageDelete(s, m.(*discordgo.MessageDelete))
if err != nil {
eMsg := f.CreateDefinedEmbed("Error (Message Deleted)", err.Error(), "error", nil)
_, err = s.ChannelMessageSendEmbed(f.Config.LoggingChannel, eMsg)
if err != nil {
log.Println("[!] Error " + err.Error())
}
}
break
case *discordgo.MessageDeleteBulk:
err := f.ProcessMessageDeleteBulk(s, m.(*discordgo.MessageDeleteBulk))
if err != nil {
eMsg := f.CreateDefinedEmbed("Error (Message Bulk Deleted)", err.Error(), "error", nil)
_, err = s.ChannelMessageSendEmbed(f.Config.LoggingChannel, eMsg)
if err != nil {
log.Println("[!] Error " + err.Error())
}
}
break
case *discordgo.GuildMemberAdd:
// Handle new member (Welcome message, etc)
err := f.ProcessUserJoin(s, m.(*discordgo.GuildMemberAdd))
if err != nil {
log.Println("[!] Error (Guild Member Joined): " + err.Error())
}
break
}
}