From afcb96a71d92e02c36ea5d8a9a13b7548b1d350c Mon Sep 17 00:00:00 2001 From: Cokybit <80327177+cokyman@users.noreply.github.com> Date: Wed, 1 Jan 2025 03:07:29 +0700 Subject: [PATCH] Add functions to automod module Add new functions to the `automod_bot.go` file to enhance the `automod` module. - **ListRulesets**: Add a function to list all rulesets in the `automod` module. - **ToggleRuleset**: Add a function to enable or disable a ruleset in the `automod` module. - **GetLogs**: Add a function to retrieve logs of triggered rules in the `automod` module. - **ListViolationsCount**: Add a function to list violations summary in the `automod` module. - **ListViolations**: Add a function to list violations of a specified user in the `automod` module. - **DeleteViolation**: Add a function to delete a violation with the specified ID in the `automod` module. - **ClearViolations**: Add a function to clear violations of a specified user in the `automod` module. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/botlabs-gg/yagpdb?shareId=XXXX-XXXX-XXXX-XXXX). --- automod/automod_bot.go | 100 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/automod/automod_bot.go b/automod/automod_bot.go index d506f6ad21..4d0b6d70c8 100644 --- a/automod/automod_bot.go +++ b/automod/automod_bot.go @@ -637,3 +637,103 @@ func handleResetChannelRatelimit(evt *schEventsModels.ScheduledEvent, data inter return false, nil } + +func (p *Plugin) ListRulesets(guildID int64) ([]*models.AutomodRuleset, error) { + rulesets, err := models.AutomodRulesets(qm.Where("guild_id = ?", guildID)).AllG(context.Background()) + if err != nil { + return nil, err + } + return rulesets, nil +} + +func (p *Plugin) ToggleRuleset(guildID int64, rulesetID int64, enable bool) error { + ruleset, err := models.FindAutomodRuleset(context.Background(), common.PQ, rulesetID) + if err != nil { + return err + } + + if ruleset.GuildID != guildID { + return errors.New("ruleset does not belong to the specified guild") + } + + ruleset.Enabled = enable + _, err = ruleset.Update(context.Background(), common.PQ, boil.Whitelist("enabled")) + if err != nil { + return err + } + + cachedRulesets.Delete(guildID) + return nil +} + +func (p *Plugin) GetLogs(guildID int64, limit int, offset int) ([]*models.AutomodTriggeredRule, error) { + logs, err := models.AutomodTriggeredRules(qm.Where("guild_id = ?", guildID), qm.OrderBy("created_at desc"), qm.Limit(limit), qm.Offset(offset)).AllG(context.Background()) + if err != nil { + return nil, err + } + return logs, nil +} + +func (p *Plugin) ListViolationsCount(guildID int64, userID int64, maxAge time.Duration) (map[string]int, error) { + qms := []qm.QueryMod{qm.Where("guild_id = ?", guildID)} + if userID != 0 { + qms = append(qms, qm.Where("user_id = ?", userID)) + } + if maxAge != 0 { + qms = append(qms, qm.Where("created_at > ?", time.Now().Add(-maxAge))) + } + + violations, err := models.AutomodViolations(qms...).AllG(context.Background()) + if err != nil { + return nil, err + } + + violationCounts := make(map[string]int) + for _, v := range violations { + violationCounts[v.Name]++ + } + + return violationCounts, nil +} + +func (p *Plugin) ListViolations(guildID int64, userID int64, limit int, offset int) ([]*models.AutomodViolation, error) { + violations, err := models.AutomodViolations(qm.Where("guild_id = ? AND user_id = ?", guildID, userID), qm.OrderBy("created_at desc"), qm.Limit(limit), qm.Offset(offset)).AllG(context.Background()) + if err != nil { + return nil, err + } + return violations, nil +} + +func (p *Plugin) DeleteViolation(guildID int64, violationID int64) error { + _, err := models.AutomodViolations(qm.Where("guild_id = ? AND id = ?", guildID, violationID)).DeleteAll(context.Background(), common.PQ) + return err +} + +func (p *Plugin) ClearViolations(guildID int64, userID int64, violationName string, maxAge time.Duration, minAge time.Duration, limit int, offset int) (int64, error) { + qms := []qm.QueryMod{qm.Where("guild_id = ?", guildID)} + if userID != 0 { + qms = append(qms, qm.Where("user_id = ?", userID)) + } + if violationName != "" { + qms = append(qms, qm.Where("name = ?", violationName)) + } + if maxAge != 0 { + qms = append(qms, qm.Where("created_at > ?", time.Now().Add(-maxAge))) + } + if minAge != 0 { + qms = append(qms, qm.Where("created_at < ?", time.Now().Add(-minAge))) + } + qms = append(qms, qm.OrderBy("created_at desc"), qm.Limit(limit), qm.Offset(offset)) + + violations, err := models.AutomodViolations(qms...).AllG(context.Background()) + if err != nil { + return 0, err + } + + deletedCount, err := violations.DeleteAll(context.Background()) + if err != nil { + return 0, err + } + + return deletedCount, nil +}