performance optimize + logic fix
This commit is contained in:
parent
22d6d7e836
commit
0d0d98a69c
@ -16,10 +16,12 @@ func DeleteCommand(data discordgo.ApplicationCommandInteractionData, s *discordg
|
||||
var wg sync.WaitGroup
|
||||
var beforeID = ""
|
||||
|
||||
OnlyWebhook, _ := strconv.ParseBool(data.Options[1].Value.(string))
|
||||
amount, _ := strconv.Atoi(data.Options[0].Value.(string))
|
||||
modulo := amount % 20
|
||||
Webhooks, _ := s.ChannelWebhooks(i.ChannelID)
|
||||
modulo := amount % 100
|
||||
|
||||
loops := math.Floor(float64(amount-modulo) / 20)
|
||||
loops := math.Floor(float64(amount-modulo) / 100)
|
||||
|
||||
err := s.InteractionRespond(
|
||||
i.Interaction,
|
||||
@ -34,7 +36,7 @@ func DeleteCommand(data discordgo.ApplicationCommandInteractionData, s *discordg
|
||||
}
|
||||
|
||||
for x := 0; x < int(loops); x++ {
|
||||
messages, err = s.ChannelMessages(i.ChannelID, 20, beforeID, "", "")
|
||||
messages, err = s.ChannelMessages(i.ChannelID, 100, beforeID, "", "")
|
||||
if err != nil {
|
||||
services.HandleError(err, s)
|
||||
}
|
||||
@ -56,10 +58,6 @@ func DeleteCommand(data discordgo.ApplicationCommandInteractionData, s *discordg
|
||||
|
||||
for _, message := range Allmessages {
|
||||
wg.Add(1)
|
||||
go func(message *discordgo.Message, i *discordgo.InteractionCreate, data discordgo.ApplicationCommandInteractionData, s *discordgo.Session) {
|
||||
var Webhooks []*discordgo.Webhook
|
||||
Webhooks, err = s.ChannelWebhooks(i.ChannelID)
|
||||
OnlyWebhook, _ := strconv.ParseBool(data.Options[1].Value.(string))
|
||||
if OnlyWebhook {
|
||||
for _, Webhook := range Webhooks {
|
||||
if message.Author.ID == Webhook.ID {
|
||||
@ -68,7 +66,6 @@ func DeleteCommand(data discordgo.ApplicationCommandInteractionData, s *discordg
|
||||
} else {
|
||||
messagesToDelete = append(messagesToDelete, message.ID)
|
||||
}
|
||||
println(message.Content + " deleted")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -77,16 +74,29 @@ func DeleteCommand(data discordgo.ApplicationCommandInteractionData, s *discordg
|
||||
} else {
|
||||
messagesToDelete = append(messagesToDelete, message.ID)
|
||||
}
|
||||
println(message.Content + " deleted")
|
||||
}
|
||||
wg.Done()
|
||||
}(message, i, data, s)
|
||||
}
|
||||
wg.Wait()
|
||||
err = s.ChannelMessagesBulkDelete(i.ChannelID, messagesToDelete)
|
||||
|
||||
modulo = len(messagesToDelete) % 100
|
||||
|
||||
loops = math.Floor(float64(len(messagesToDelete)-modulo) / 100)
|
||||
|
||||
for x := 0; x < int(loops); x++ {
|
||||
index := x * 100
|
||||
err = s.ChannelMessagesBulkDelete(i.ChannelID, messagesToDelete[index:index+100])
|
||||
if err != nil {
|
||||
services.HandleError(err, s)
|
||||
}
|
||||
}
|
||||
if modulo > 0 {
|
||||
index := int(loops) * 100
|
||||
err = s.ChannelMessagesBulkDelete(i.ChannelID, messagesToDelete[index:index+modulo])
|
||||
if err != nil {
|
||||
services.HandleError(err, s)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
|
||||
Content: strconv.Itoa(len(Allmessages)) + " Message deleted",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user