2021-05-29 12:17:01 +00:00
|
|
|
package commands
|
2020-04-11 14:16:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-05-25 06:32:23 +00:00
|
|
|
"time"
|
2021-10-01 13:12:04 +00:00
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"github.com/foxtrot/scuzzy/actions"
|
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 {
|
2021-10-01 14:58:19 +00:00
|
|
|
currPos := channel.Position
|
2021-05-28 15:40:51 +00:00
|
|
|
s.ChannelEditComplex(channel.ID, &discordgo.ChannelEdit{
|
2021-10-01 14:58:19 +00:00
|
|
|
Position: currPos,
|
2022-03-12 20:30:33 +00:00
|
|
|
RateLimitPerUser: slowModeTime,
|
2021-05-28 15:40:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-05-28 15:51:34 +00:00
|
|
|
} else {
|
2021-10-01 14:58:19 +00:00
|
|
|
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{
|
2021-10-01 14:58:19 +00:00
|
|
|
Position: currPos,
|
2022-03-12 20:30:33 +00:00
|
|
|
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, " ")
|
|
|
|
|
2021-10-01 14:58:19 +00:00
|
|
|
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 {
|
2021-10-01 14:58:19 +00:00
|
|
|
currPos := channel.Position
|
2021-05-28 15:51:34 +00:00
|
|
|
s.ChannelEditComplex(channel.ID, &discordgo.ChannelEdit{
|
2021-10-01 14:58:19 +00:00
|
|
|
Position: currPos,
|
2022-03-12 20:30:33 +00:00
|
|
|
RateLimitPerUser: secs,
|
2021-05-28 15:51:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-01 14:58:19 +00:00
|
|
|
currChan, err := s.Channel(m.ChannelID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
currPos := currChan.Position
|
|
|
|
_, err = s.ChannelEditComplex(m.ChannelID, &discordgo.ChannelEdit{
|
|
|
|
Position: currPos,
|
2022-03-12 20:30:33 +00:00
|
|
|
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 {
|
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)
|
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)
|
2020-05-25 06:32:23 +00:00
|
|
|
msgS, err := s.ChannelMessageSendEmbed(m.ChannelID, msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-25 22:46:15 +00:00
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
|
|
|
|
err = s.ChannelMessageDelete(m.ChannelID, msgS.ID)
|
2020-04-11 14:16:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-08 06:33:08 +00:00
|
|
|
|
2021-05-29 12:17:01 +00:00
|
|
|
func (c *Commands) handleKickUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
|
2020-05-08 06:33:08 +00:00
|
|
|
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:], " ")
|
2020-05-08 06:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
member := args[1]
|
2020-07-19 01:19:17 +00:00
|
|
|
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)
|
2020-05-08 06:33:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-01 13:12:04 +00:00
|
|
|
err = actions.KickUser(s, c.Config.GuildID, mHandle.User.ID, kickReason)
|
2020-05-08 06:33:08 +00:00
|
|
|
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)
|
2020-05-08 06:33:08 +00:00
|
|
|
_, 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 {
|
2020-05-08 06:33:08 +00:00
|
|
|
var (
|
2020-05-25 06:28:38 +00:00
|
|
|
mHandle *discordgo.User
|
2020-05-08 06:33:08 +00:00
|
|
|
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:], " ")
|
2020-05-08 06:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
member := args[1]
|
2020-07-19 01:19:17 +00:00
|
|
|
idStr := strings.ReplaceAll(member, "<@!", "")
|
|
|
|
idStr = strings.ReplaceAll(idStr, "<@", "")
|
|
|
|
idStr = strings.ReplaceAll(idStr, ">", "")
|
2020-05-25 06:28:38 +00:00
|
|
|
mHandle, err = s.User(idStr)
|
2020-05-08 06:33:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-01 14:58:19 +00:00
|
|
|
err = actions.BanUser(s, c.Config.GuildID, mHandle.ID, banReason)
|
2020-05-08 06:33:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-25 06:28:38 +00:00
|
|
|
msg := "User `" + mHandle.Username + "#" + mHandle.Discriminator + "` was banned.\n"
|
2020-05-08 06:33:08 +00:00
|
|
|
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)
|
2020-05-08 06:33:08 +00:00
|
|
|
_, err = s.ChannelMessageSendEmbed(m.ChannelID, embed)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-25 22:46:15 +00:00
|
|
|
|
2021-05-29 12:17:01 +00:00
|
|
|
func (c *Commands) handleIgnoreUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
|
2020-05-25 22:46:15 +00:00
|
|
|
ignArgs := strings.Split(m.Content, " ")
|
|
|
|
if len(ignArgs) < 2 {
|
|
|
|
return errors.New("You did not specify a user.")
|
|
|
|
}
|
|
|
|
|
|
|
|
member := ignArgs[1]
|
2020-07-19 01:19:17 +00:00
|
|
|
idStr := strings.ReplaceAll(member, "<@!", "")
|
|
|
|
idStr = strings.ReplaceAll(idStr, "<@", "")
|
|
|
|
idStr = strings.ReplaceAll(idStr, ">", "")
|
2020-05-25 22:46:15 +00:00
|
|
|
|
2021-05-29 12:17:01 +00:00
|
|
|
c.Config.IgnoredUsers = append(c.Config.IgnoredUsers, idStr)
|
2020-05-25 22:46:15 +00:00
|
|
|
|
2021-05-29 12:17:01 +00:00
|
|
|
eMsg := c.CreateDefinedEmbed("Ignore User", "<@!"+idStr+"> is now being ignored.", "success", m.Author)
|
2020-05-25 22:46:15 +00:00
|
|
|
_, err := s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-29 12:17:01 +00:00
|
|
|
err = c.handleSaveConfig(s, m)
|
2020-05-25 22:46:15 +00:00
|
|
|
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 {
|
2020-05-25 22:46:15 +00:00
|
|
|
ignArgs := strings.Split(m.Content, " ")
|
|
|
|
if len(ignArgs) < 2 {
|
|
|
|
return errors.New("You did not specify a user.")
|
|
|
|
}
|
|
|
|
|
|
|
|
member := ignArgs[1]
|
2020-07-19 01:19:17 +00:00
|
|
|
idStr := strings.ReplaceAll(member, "<@!", "")
|
|
|
|
idStr = strings.ReplaceAll(idStr, "<@", "")
|
|
|
|
idStr = strings.ReplaceAll(idStr, ">", "")
|
2020-05-25 22:46:15 +00:00
|
|
|
|
2021-05-29 12:17:01 +00:00
|
|
|
for k, v := range c.Config.IgnoredUsers {
|
2020-05-25 22:46:15 +00:00
|
|
|
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]
|
2020-05-25 22:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-29 12:17:01 +00:00
|
|
|
eMsg := c.CreateDefinedEmbed("Unignore User", "<@!"+idStr+"> is not being ignored.", "success", m.Author)
|
2020-05-25 22:46:15 +00:00
|
|
|
_, err := s.ChannelMessageSendEmbed(m.ChannelID, eMsg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-29 12:17:01 +00:00
|
|
|
err = c.handleSaveConfig(s, m)
|
2020-05-25 22:46:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|