scuzzy/commands/moderation.go

307 lines
7.2 KiB
Go
Raw Normal View History

2021-05-29 12:17:01 +00:00
package commands
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
import (
"errors"
"strconv"
"strings"
"time"
2021-10-01 13:12:04 +00:00
"github.com/bwmarrin/discordgo"
"github.com/foxtrot/scuzzy/actions"
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
)
2021-05-29 12:17:01 +00:00
func (c *Commands) handleSetSlowmode(s *discordgo.Session, m *discordgo.MessageCreate) error {
2021-05-28 15:40:51 +00:00
slowmodeSplit := strings.Split(m.Content, " ")
if len(slowmodeSplit) < 2 {
return errors.New("You must supply at least an amount of time")
}
2021-05-28 15:51:34 +00:00
slowmodeTimeStr := slowmodeSplit[1]
slowModeTime, err := strconv.Atoi(slowmodeTimeStr)
if err != nil {
return err
}
2021-05-28 15:40:51 +00:00
if len(slowmodeSplit) == 3 {
if slowmodeSplit[2] == "all" {
2021-05-29 12:17:01 +00:00
channels, err := s.GuildChannels(c.Config.GuildID)
2021-05-28 15:40:51 +00:00
if err != nil {
return err
}
for _, channel := range channels {
currPos := channel.Position
2021-05-28 15:40:51 +00:00
s.ChannelEditComplex(channel.ID, &discordgo.ChannelEdit{
Position: currPos,
RateLimitPerUser: slowModeTime,
2021-05-28 15:40:51 +00:00
})
}
}
2021-05-28 15:51:34 +00:00
} else {
currChan, err := s.Channel(m.ChannelID)
if err != nil {
return err
}
currPos := currChan.Position
2021-05-28 16:23:25 +00:00
_, err = s.ChannelEditComplex(m.ChannelID, &discordgo.ChannelEdit{
Position: currPos,
RateLimitPerUser: slowModeTime,
2021-05-28 15:51:34 +00:00
})
if err != nil {
return err
}
2021-05-28 15:40:51 +00:00
}
2021-05-29 12:17:01 +00:00
msg := c.CreateDefinedEmbed("Slow Mode", "Successfully set Slow Mode to `"+slowmodeTimeStr+"`.", "success", m.Author)
2021-05-28 15:51:34 +00:00
_, err = s.ChannelMessageSendEmbed(m.ChannelID, msg)
2021-05-28 15:40:51 +00:00
if err != nil {
return err
}
2021-05-28 15:51:34 +00:00
return nil
}
2021-05-29 12:17:01 +00:00
func (c *Commands) handleUnsetSlowmode(s *discordgo.Session, m *discordgo.MessageCreate) error {
2021-05-28 15:51:34 +00:00
slowmodeSplit := strings.Split(m.Content, " ")
secs := 0
2021-05-28 15:51:34 +00:00
if len(slowmodeSplit) == 2 {
if slowmodeSplit[1] == "all" {
2021-05-29 12:17:01 +00:00
channels, err := s.GuildChannels(c.Config.GuildID)
2021-05-28 15:51:34 +00:00
if err != nil {
return err
}
for _, channel := range channels {
currPos := channel.Position
2021-05-28 15:51:34 +00:00
s.ChannelEditComplex(channel.ID, &discordgo.ChannelEdit{
Position: currPos,
RateLimitPerUser: secs,
2021-05-28 15:51:34 +00:00
})
}
}
} else {
currChan, err := s.Channel(m.ChannelID)
if err != nil {
return err
}
currPos := currChan.Position
_, err = s.ChannelEditComplex(m.ChannelID, &discordgo.ChannelEdit{
Position: currPos,
RateLimitPerUser: secs,
2021-05-28 15:51:34 +00:00
})
if err != nil {
return err
}
}
2021-05-29 12:17:01 +00:00
msg := c.CreateDefinedEmbed("Slow Mode", "Successfully unset Slow Mode", "success", m.Author)
2021-05-28 15:51:34 +00:00
_, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
2021-05-28 15:40:51 +00:00
if err != nil {
return err
}
return nil
}
2021-05-29 12:17:01 +00:00
func (c *Commands) handlePurgeChannel(s *discordgo.Session, m *discordgo.MessageCreate) error {
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
purgeSplit := strings.SplitN(m.Content, " ", 2)
if len(purgeSplit) < 2 {
return errors.New("No message count supplied")
}
msgCount, err := strconv.Atoi(purgeSplit[1])
if err != nil {
return nil
}
if msgCount > 100 {
return errors.New("You may only purge upto 100 messages at a time.")
}
chanMsgs, err := s.ChannelMessages(m.ChannelID, msgCount, "", "", "")
if err != nil {
return err
}
2021-05-29 12:17:01 +00:00
msg := c.CreateDefinedEmbed("Purge Channel", "Purging `"+purgeSplit[1]+"` messages.", "", m.Author)
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
r, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
}
var delMsgs []string
for _, v := range chanMsgs {
delMsgs = append(delMsgs, v.ID)
}
err = s.ChannelMessagesBulkDelete(m.ChannelID, delMsgs)
if err != nil {
return err
}
err = s.ChannelMessageDelete(m.ChannelID, r.ID)
2021-05-29 12:17:01 +00:00
msg = c.CreateDefinedEmbed("Purge Channel", "Purged `"+purgeSplit[1]+"` messages!", "success", m.Author)
msgS, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
if err != nil {
return err
}
time.Sleep(time.Second * 10)
err = s.ChannelMessageDelete(m.ChannelID, msgS.ID)
Create Scuzzy Features: Hastily add usercolor feature Main: Show prefix in bot status Features: add cat Main/Features/Auth: Add basic Role Name/ID based command authentication and bot configuration. Main: Sanity check for missing flags. Features: Implement command deletion and tag-responses Misc: Add cmd/main to .gitignore Misc: Delete built binary Main: Refactor main.go - Use log.Fatal instead of fmt.Printf and os.Exit - Move Config reading into seperate function - Parse command line flags in main() - Instantiate Features.go struct inside main() - Use simple ticker to set bot status every 15 minutes Auth: Refactor auth - Return early when building Admin role list - Move CheckAdminrole() into auth.go, remove roles.go Features: Refactor features - Remove New() function - Move help.go, ping.go functionality into misc.go Features: Add bot control feature to set status. Features: Implement helper function for embeds, return errors. Main: Rename config file to bot_config.json Features: Use success embed for user color. Features: Add purge messages feature. Main: Add status_text property to config, use it for Bot Status. Features: Send welcome message to new users, implement auto-join roles. Main: Use complex user status Main: Reduce ticker interval to 10 minutes for bot status update. Main: Create helper function for complex custom embeds. Features: Implement info command. Features: Convert requested user color to lowercase. Features: Add markdown helper Features: Fix markdown helper typo Features: Use error embed for handleSetStatus incorrect permissions. Features: Add strikethrough and spoilers information to markdown info. Features: Add info and md to help text. Features: Use backtick instead of single quote for command prefix in help text. Features: Remove markdown info message after 15 seconds. Features: Fix typo in status setting permission error. Features: Add commands to convert between Celsius and Farenheit Features: Update help to include temperature conversion commands. Features/Models/Main: Load color roles from JSON config. Auth/Main/Features/Models: Implement config-based channel white/blacklisting. Main/Models: Remove unused admin_only config field. Features: Simplify error handling. Auth: Remove leftover AdminOnly field. Features: Use command restriction for usercolor commands. Main: Update default config to restrict color and colors commands. Auth/Features: Return true for undefined commands in restrictions list by default. Features: Use better error messages for channel white/blacklist Auth: Improve channel white/blacklist behavior. Main: Replace broken ticker for status updates Features: Get bot avatar via Discord for embed. Features: Delete requesting message for markdown info. Features: Handle potential error in deleting help requester message.
2020-04-11 14:16:09 +00:00
if err != nil {
return err
}
return nil
}
2021-05-29 12:17:01 +00:00
func (c *Commands) handleKickUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
var (
mHandle *discordgo.Member
kickReason string
err error
)
args := strings.Split(m.Content, " ")
if len(args) < 2 {
return errors.New("You must specify a user to kick.")
}
if len(args) == 3 {
2021-10-01 13:12:04 +00:00
kickReason = strings.Join(args[2:], " ")
}
member := args[1]
idStr := strings.ReplaceAll(member, "<@!", "")
idStr = strings.ReplaceAll(idStr, "<@", "")
idStr = strings.ReplaceAll(idStr, ">", "")
2021-05-29 12:17:01 +00:00
mHandle, err = s.GuildMember(c.Config.GuildID, idStr)
if err != nil {
return err
}
2021-10-01 13:12:04 +00:00
err = actions.KickUser(s, c.Config.GuildID, mHandle.User.ID, kickReason)
if err != nil {
return err
}
msg := "User `" + mHandle.User.Username + "#" + mHandle.User.Discriminator + "` was kicked.\n"
if len(kickReason) > 0 {
msg += "Reason: `" + kickReason + "`\n"
}
2021-05-29 12:17:01 +00:00
embed := c.CreateDefinedEmbed("Kick User", msg, "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, embed)
if err != nil {
return err
}
return nil
}
2021-05-29 12:17:01 +00:00
func (c *Commands) handleBanUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
var (
mHandle *discordgo.User
banReason string
err error
)
args := strings.Split(m.Content, " ")
if len(args) < 2 {
return errors.New("You must specify a user to ban.")
}
if len(args) == 3 {
2021-10-01 13:12:04 +00:00
banReason = strings.Join(args[2:], " ")
}
member := args[1]
idStr := strings.ReplaceAll(member, "<@!", "")
idStr = strings.ReplaceAll(idStr, "<@", "")
idStr = strings.ReplaceAll(idStr, ">", "")
mHandle, err = s.User(idStr)
if err != nil {
return err
}
err = actions.BanUser(s, c.Config.GuildID, mHandle.ID, banReason)
if err != nil {
return err
}
msg := "User `" + mHandle.Username + "#" + mHandle.Discriminator + "` was banned.\n"
if len(banReason) > 0 {
msg += "Reason: `" + banReason + "`\n"
}
2021-05-29 12:17:01 +00:00
embed := c.CreateDefinedEmbed("Ban User", msg, "success", m.Author)
_, err = s.ChannelMessageSendEmbed(m.ChannelID, embed)
if err != nil {
return err
}
return nil
}
2021-05-29 12:17:01 +00:00
func (c *Commands) handleIgnoreUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
ignArgs := strings.Split(m.Content, " ")
if len(ignArgs) < 2 {
return errors.New("You did not specify a user.")
}
member := ignArgs[1]
idStr := strings.ReplaceAll(member, "<@!", "")
idStr = strings.ReplaceAll(idStr, "<@", "")
idStr = strings.ReplaceAll(idStr, ">", "")
2021-05-29 12:17:01 +00:00
c.Config.IgnoredUsers = append(c.Config.IgnoredUsers, idStr)
2021-05-29 12:17:01 +00:00
eMsg := c.CreateDefinedEmbed("Ignore User", "<@!"+idStr+"> is now being ignored.", "success", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
if err != nil {
return err
}
2021-05-29 12:17:01 +00:00
err = c.handleSaveConfig(s, m)
if err != nil {
return err
}
return nil
}
2021-05-29 12:17:01 +00:00
func (c *Commands) handleUnIgnoreUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
ignArgs := strings.Split(m.Content, " ")
if len(ignArgs) < 2 {
return errors.New("You did not specify a user.")
}
member := ignArgs[1]
idStr := strings.ReplaceAll(member, "<@!", "")
idStr = strings.ReplaceAll(idStr, "<@", "")
idStr = strings.ReplaceAll(idStr, ">", "")
2021-05-29 12:17:01 +00:00
for k, v := range c.Config.IgnoredUsers {
if v == idStr {
2021-05-29 12:17:01 +00:00
c.Config.IgnoredUsers[k] = c.Config.IgnoredUsers[len(c.Config.IgnoredUsers)-1]
c.Config.IgnoredUsers = c.Config.IgnoredUsers[:len(c.Config.IgnoredUsers)-1]
}
}
2021-05-29 12:17:01 +00:00
eMsg := c.CreateDefinedEmbed("Unignore User", "<@!"+idStr+"> is not being ignored.", "success", m.Author)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
if err != nil {
return err
}
2021-05-29 12:17:01 +00:00
err = c.handleSaveConfig(s, m)
if err != nil {
return err
}
return nil
}