2020-06-09 13:04:44 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const clearMax = 20
|
|
|
|
|
|
|
|
func init() {
|
2021-02-23 05:53:55 +00:00
|
|
|
commands = append(commands, &command{
|
|
|
|
name: "clear",
|
2021-05-14 03:04:34 +00:00
|
|
|
staffOnly: true,
|
2020-06-09 13:04:44 +00:00
|
|
|
validate: func(cmd commandInit) bool {
|
|
|
|
return isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles)
|
|
|
|
},
|
2020-06-12 05:17:58 +00:00
|
|
|
run: func(cmd commandInit) (string, error) {
|
2020-06-09 13:04:44 +00:00
|
|
|
args := strings.Fields(cmd.message.Content)
|
|
|
|
|
|
|
|
var userID string
|
|
|
|
if len(cmd.message.Mentions) > 0 {
|
|
|
|
userID = cmd.message.Mentions[0].ID
|
|
|
|
}
|
|
|
|
|
|
|
|
limitArg := 1
|
|
|
|
if userID != "" {
|
|
|
|
limitArg = 2
|
|
|
|
if len(args) < 2 {
|
2021-01-09 03:00:28 +00:00
|
|
|
return "This command takes two arguments with a mention", nil
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
} else if len(args) < 1 {
|
2020-06-12 05:17:58 +00:00
|
|
|
return "This command takes one argument without a mention", nil
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
limit, err := strconv.Atoi(args[limitArg])
|
|
|
|
if err != nil {
|
2020-06-12 05:17:58 +00:00
|
|
|
return "", err
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if limit < 0 {
|
|
|
|
limit = 0
|
|
|
|
} else if limit > clearMax {
|
|
|
|
limit = 20
|
|
|
|
}
|
|
|
|
|
|
|
|
batch := []string{cmd.message.ID}
|
|
|
|
|
|
|
|
var deleted int
|
|
|
|
messages, err := cmd.session.ChannelMessages(cmd.message.ChannelID, 100, cmd.message.ID, "", "")
|
|
|
|
for _, msg := range messages {
|
|
|
|
if deleted == limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteMessage := true
|
|
|
|
if userID != "" {
|
|
|
|
deleteMessage = msg.Author.ID == userID
|
|
|
|
}
|
|
|
|
|
|
|
|
if deleteMessage {
|
|
|
|
batch = append(batch, msg.ID)
|
|
|
|
deleted++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 05:17:58 +00:00
|
|
|
return "", cmd.session.ChannelMessagesBulkDelete(cmd.message.ChannelID, batch)
|
2020-06-09 13:04:44 +00:00
|
|
|
},
|
|
|
|
help: "Clear messages",
|
2021-02-23 05:53:55 +00:00
|
|
|
})
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|