2020-04-11 14:16:09 +00:00
|
|
|
package features
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/bwmarrin/discord.go"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-05-25 06:32:23 +00:00
|
|
|
"time"
|
2020-04-11 14:16:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (f *Features) handlePurgeChannel(s *discordgo.Session, m *discordgo.MessageCreate) error {
|
2020-05-25 22:46:15 +00:00
|
|
|
if !f.Permissions.CheckAdminRole(m.Member) {
|
2020-04-11 14:16:09 +00:00
|
|
|
return errors.New("You do not have permissions to use that command.")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:31:26 +00:00
|
|
|
msg := f.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)
|
2020-04-27 18:31:26 +00:00
|
|
|
msg = f.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
|
|
|
|
|
|
|
func (f *Features) handleKickUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
|
2020-05-25 22:46:15 +00:00
|
|
|
if !f.Permissions.CheckAdminRole(m.Member) {
|
2020-05-08 06:33:08 +00:00
|
|
|
return errors.New("You do not have permissions to use this command.")
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
kickReason = args[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08 06:33:08 +00:00
|
|
|
mHandle, err = s.GuildMember(f.Config.Guild.ID, idStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.GuildMemberDeleteWithReason(f.Config.Guild.ID, 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"
|
|
|
|
}
|
|
|
|
|
|
|
|
embed := f.CreateDefinedEmbed("Kick User", msg, "success", m.Author)
|
|
|
|
_, err = s.ChannelMessageSendEmbed(m.ChannelID, embed)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Features) handleBanUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
|
2020-05-25 22:46:15 +00:00
|
|
|
if !f.Permissions.CheckAdminRole(m.Member) {
|
2020-05-08 06:33:08 +00:00
|
|
|
return errors.New("You do not have permissions to use this command.")
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
banReason = args[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-25 06:28:38 +00:00
|
|
|
err = s.GuildBanCreateWithReason(f.Config.Guild.ID, mHandle.ID, banReason, 0)
|
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"
|
|
|
|
}
|
|
|
|
|
|
|
|
embed := f.CreateDefinedEmbed("Ban User", msg, "success", m.Author)
|
|
|
|
_, err = s.ChannelMessageSendEmbed(m.ChannelID, embed)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-25 22:46:15 +00:00
|
|
|
|
|
|
|
func (f *Features) handleIgnoreUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
|
|
|
|
if !f.Permissions.CheckAdminRole(m.Member) {
|
|
|
|
return errors.New("You do not have permissions to use this command.")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
f.Config.IgnoredUsers = append(f.Config.IgnoredUsers, idStr)
|
|
|
|
|
|
|
|
eMsg := f.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)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Features) handleUnIgnoreUser(s *discordgo.Session, m *discordgo.MessageCreate) error {
|
|
|
|
if !f.Permissions.CheckAdminRole(m.Member) {
|
|
|
|
return errors.New("You do not have permissions to use this command.")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
for k, v := range f.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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eMsg := f.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)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|