<commit_before>package plugins

import (
	"fmt"
	"strings"
	"time"

	"github.com/Seklfreak/Robyul2/cache"
	"github.com/Seklfreak/Robyul2/helpers"
	"github.com/Seklfreak/Robyul2/models"
	"github.com/bwmarrin/discordgo"
	rethink "github.com/gorethink/gorethink"
)

type Troublemaker struct{}

func (t *Troublemaker) Commands() []string {
	return []string{
		"troublemaker",
		"troublemakers",
		"tm",
	}
}

type DB_Troublemaker_Entry struct {
	ID                string    `gorethink:"id,omitempty"`
	UserID            string    `gorethink:"userid"`
	Reason            string    `gorethink:"reason"`
	CreatedAt         time.Time `gorethink:"createdat"`
	ReportedByGuildID string    `gorethink:"reportedby_guildid"`
	ReportedByUserID  string    `gorethink:"reportedby_userid"`
}

func (t *Troublemaker) Init(session *discordgo.Session) {

}

func (t *Troublemaker) Action(command string, content string, msg *discordgo.Message, session *discordgo.Session) {
	if !helpers.ModuleIsAllowed(msg.ChannelID, msg.ID, msg.Author.ID, helpers.ModulePermTroublemaker) {
		return
	}

	args := strings.Fields(content)
	if len(args) >= 1 {
		switch args[0] {
		case "participate":
			helpers.RequireAdmin(msg, func() {
				session.ChannelTyping(msg.ChannelID)

				channel, err := helpers.GetChannel(msg.ChannelID)
				helpers.Relax(err)

				settings := helpers.GuildSettingsGetCached(channel.GuildID)

				if len(args) >= 2 {
					// Set new log channel
					targetChannel, err := helpers.GetChannelFromMention(msg, args[1])
					if err != nil || targetChannel.ID == "" {
						helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
						return
					}

					previousChannel := settings.TroublemakerLogChannel

					settings.TroublemakerIsParticipating = true
					settings.TroublemakerLogChannel = targetChannel.ID
					err = helpers.GuildSettingsSet(channel.GuildID, settings)
					helpers.Relax(err)

					changes := make([]models.ElasticEventlogChange, 0)
					if previousChannel != "" {
						changes = []models.ElasticEventlogChange{
							{
								Key:      "troublemaker_participate_channel",
								OldValue: previousChannel,
								NewValue: settings.TroublemakerLogChannel,
							},
						}
					}

					changes = append(changes, models.ElasticEventlogChange{
						Key:      "troublemaker_participating",
						OldValue: helpers.StoreBoolAsString(false),
						NewValue: helpers.StoreBoolAsString(true),
					})

					_, err = helpers.EventlogLog(time.Now(), channel.GuildID, channel.GuildID,
						models.EventlogTargetTypeGuild, msg.Author.ID,
						models.EventlogTypeRobyulTroublemakerParticipate, "",
						changes,
						[]models.ElasticEventlogOption{
							{
								Key:   "troublemaker_participate_channel",
								Value: targetChannel.ID,
							},
						}, false)
					helpers.RelaxLog(err)

					_, err = helpers.SendMessage(msg.ChannelID, helpers.GetText("plugins.troublemaker.participation-enabled"))
					helpers.Relax(err)

					return
				} else {
					// Disable logging
					settings.TroublemakerIsParticipating = false
					err = helpers.GuildSettingsSet(channel.GuildID, settings)
					helpers.Relax(err)

					_, err = helpers.EventlogLog(time.Now(), channel.GuildID, channel.GuildID,
						models.EventlogTargetTypeGuild, msg.Author.ID,
						models.EventlogTypeRobyulTroublemakerParticipate, "",
						[]models.ElasticEventlogChange{
							{
								Key:      "troublemaker_participating",
								OldValue: helpers.StoreBoolAsString(true),
								NewValue: helpers.StoreBoolAsString(false),
							},
						},
						nil, false)
					helpers.RelaxLog(err)

					_, err = helpers.SendMessage(msg.ChannelID, helpers.GetText("plugins.troublemaker.participation-disabled"))
					helpers.Relax(err)

					return
				}
				return
			})
			break
		case "list":
			helpers.RequireMod(msg, func() {
				session.ChannelTyping(msg.ChannelID)

				if len(args) < 2 {
					helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
					return
				}

				targetUser, err := helpers.GetUserFromMention(args[1])
				if err != nil || targetUser.ID == "" {
					helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
					return
				}

				troublemakerReports := t.getTroublemakerReports(targetUser)

				if len(troublemakerReports) <= 0 {
					_, err := helpers.SendMessage(msg.ChannelID, helpers.GetTextF("plugins.troublemaker.list-no-reports", targetUser.Username))
					helpers.Relax(err)
					return
				} else {
					troublemakerText := fmt.Sprintf("I found %d report(s) for `%s#%s` `#%s` <@%s>:\n",
						len(troublemakerReports), targetUser.Username, targetUser.Discriminator, targetUser.ID, targetUser.ID,
					)

					for _, troublemakerReport := range troublemakerReports {
						reportedByUser, err := helpers.GetUser(troublemakerReport.ReportedByUserID)
						if err != nil {
							reportedByUser = new(discordgo.User)
							reportedByUser.ID = troublemakerReport.ReportedByUserID
							reportedByUser.Username = "N/A"
						}
						reportedByGuild, err := helpers.GetGuild(troublemakerReport.ReportedByGuildID)
						if err != nil {
							reportedByGuild = new(discordgo.Guild)
							reportedByGuild.ID = troublemakerReport.ReportedByGuildID
							reportedByGuild.Name = "N/A"
						}
						troublemakerText += fmt.Sprintf("At: `%s`, Reason: `%s`, Reported By: `%s` (`#%s`) On: `%s` (`#%s`)\n",
							troublemakerReport.CreatedAt.Format(time.ANSIC), troublemakerReport.Reason,
							reportedByUser.Username, reportedByUser.ID, reportedByGuild.Name, reportedByGuild.ID,
						)
					}

					for _, page := range helpers.Pagify(troublemakerText, "\n") {
						_, err := helpers.SendMessage(msg.ChannelID, page)
						helpers.Relax(err)
					}
				}
			})
		default:
			helpers.RequireMod(msg, func() {
				session.ChannelTyping(msg.ChannelID)

				channel, err := helpers.GetChannel(msg.ChannelID)
				helpers.Relax(err)
				guild, err := helpers.GetGuild(channel.GuildID)
				helpers.Relax(err)

				if len(args) < 2 {
					helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
					return
				}

				targetUser, err := helpers.GetUserFromMention(args[0])
				if err != nil || targetUser.ID == "" {
					helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
					return
				}

				reasonText := strings.TrimSpace(strings.Replace(content, strings.Join(args[:1], " "), "", 1))

				if helpers.ConfirmEmbed(msg.ChannelID, msg.Author, helpers.GetTextF("plugins.troublemaker.report-confirm",
					targetUser.Username, targetUser.Discriminator, targetUser.ID, targetUser.ID, reasonText,
				), "✅", "🚫") == true {
					// Save to log DB
					troublemakerLogEntry := t.getEntryByOrCreateEmpty("id", "")
					troublemakerLogEntry.UserID = targetUser.ID
					troublemakerLogEntry.Reason = reasonText
					troublemakerLogEntry.CreatedAt = time.Now()
					troublemakerLogEntry.ReportedByGuildID = guild.ID<commit_msg>[troublemaker] allows list for basic and extended inspect mods<commit_after>package plugins

import (
	"fmt"
	"strings"
	"time"

	"github.com/Seklfreak/Robyul2/cache"
	"github.com/Seklfreak/Robyul2/helpers"
	"github.com/Seklfreak/Robyul2/models"
	"github.com/bwmarrin/discordgo"
	rethink "github.com/gorethink/gorethink"
)

type Troublemaker struct{}

func (t *Troublemaker) Commands() []string {
	return []string{
		"troublemaker",
		"troublemakers",
		"tm",
	}
}

type DB_Troublemaker_Entry struct {
	ID                string    `gorethink:"id,omitempty"`
	UserID            string    `gorethink:"userid"`
	Reason            string    `gorethink:"reason"`
	CreatedAt         time.Time `gorethink:"createdat"`
	ReportedByGuildID string    `gorethink:"reportedby_guildid"`
	ReportedByUserID  string    `gorethink:"reportedby_userid"`
}

func (t *Troublemaker) Init(session *discordgo.Session) {

}

func (t *Troublemaker) Action(command string, content string, msg *discordgo.Message, session *discordgo.Session) {
	if !helpers.ModuleIsAllowed(msg.ChannelID, msg.ID, msg.Author.ID, helpers.ModulePermTroublemaker) {
		return
	}

	args := strings.Fields(content)
	if len(args) >= 1 {
		switch args[0] {
		case "participate":
			helpers.RequireAdmin(msg, func() {
				session.ChannelTyping(msg.ChannelID)

				channel, err := helpers.GetChannel(msg.ChannelID)
				helpers.Relax(err)

				settings := helpers.GuildSettingsGetCached(channel.GuildID)

				if len(args) >= 2 {
					// Set new log channel
					targetChannel, err := helpers.GetChannelFromMention(msg, args[1])
					if err != nil || targetChannel.ID == "" {
						helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
						return
					}

					previousChannel := settings.TroublemakerLogChannel

					settings.TroublemakerIsParticipating = true
					settings.TroublemakerLogChannel = targetChannel.ID
					err = helpers.GuildSettingsSet(channel.GuildID, settings)
					helpers.Relax(err)

					changes := make([]models.ElasticEventlogChange, 0)
					if previousChannel != "" {
						changes = []models.ElasticEventlogChange{
							{
								Key:      "troublemaker_participate_channel",
								OldValue: previousChannel,
								NewValue: settings.TroublemakerLogChannel,
							},
						}
					}

					changes = append(changes, models.ElasticEventlogChange{
						Key:      "troublemaker_participating",
						OldValue: helpers.StoreBoolAsString(false),
						NewValue: helpers.StoreBoolAsString(true),
					})

					_, err = helpers.EventlogLog(time.Now(), channel.GuildID, channel.GuildID,
						models.EventlogTargetTypeGuild, msg.Author.ID,
						models.EventlogTypeRobyulTroublemakerParticipate, "",
						changes,
						[]models.ElasticEventlogOption{
							{
								Key:   "troublemaker_participate_channel",
								Value: targetChannel.ID,
							},
						}, false)
					helpers.RelaxLog(err)

					_, err = helpers.SendMessage(msg.ChannelID, helpers.GetText("plugins.troublemaker.participation-enabled"))
					helpers.Relax(err)

					return
				} else {
					// Disable logging
					settings.TroublemakerIsParticipating = false
					err = helpers.GuildSettingsSet(channel.GuildID, settings)
					helpers.Relax(err)

					_, err = helpers.EventlogLog(time.Now(), channel.GuildID, channel.GuildID,
						models.EventlogTargetTypeGuild, msg.Author.ID,
						models.EventlogTypeRobyulTroublemakerParticipate, "",
						[]models.ElasticEventlogChange{
							{
								Key:      "troublemaker_participating",
								OldValue: helpers.StoreBoolAsString(true),
								NewValue: helpers.StoreBoolAsString(false),
							},
						},
						nil, false)
					helpers.RelaxLog(err)

					_, err = helpers.SendMessage(msg.ChannelID, helpers.GetText("plugins.troublemaker.participation-disabled"))
					helpers.Relax(err)

					return
				}
				return
			})
			break
		case "list":
			if !helpers.IsMod(msg) && !helpers.CanInspectExtended(msg) && !helpers.CanInspectBasic(msg) {
				helpers.SendMessage(msg.ChannelID, helpers.GetText("mod.no_permission"))
				return
			}

			session.ChannelTyping(msg.ChannelID)

			if len(args) < 2 {
				helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
				return
			}

			targetUser, err := helpers.GetUserFromMention(args[1])
			if err != nil || targetUser.ID == "" {
				helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
				return
			}

			troublemakerReports := t.getTroublemakerReports(targetUser)

			if len(troublemakerReports) <= 0 {
				_, err := helpers.SendMessage(msg.ChannelID, helpers.GetTextF("plugins.troublemaker.list-no-reports", targetUser.Username))
				helpers.Relax(err)
				return
			} else {
				troublemakerText := fmt.Sprintf("I found %d report(s) for `%s#%s` `#%s` <@%s>:\n",
					len(troublemakerReports), targetUser.Username, targetUser.Discriminator, targetUser.ID, targetUser.ID,
				)

				for _, troublemakerReport := range troublemakerReports {
					reportedByUser, err := helpers.GetUser(troublemakerReport.ReportedByUserID)
					if err != nil {
						reportedByUser = new(discordgo.User)
						reportedByUser.ID = troublemakerReport.ReportedByUserID
						reportedByUser.Username = "<PASSWORD>"
					}
					reportedByGuild, err := helpers.GetGuild(troublemakerReport.ReportedByGuildID)
					if err != nil {
						reportedByGuild = new(discordgo.Guild)
						reportedByGuild.ID = troublemakerReport.ReportedByGuildID
						reportedByGuild.Name = "N/A"
					}
					troublemakerText += fmt.Sprintf("At: `%s`, Reason: `%s`, Reported By: `%s` (`#%s`) On: `%s` (`#%s`)\n",
						troublemakerReport.CreatedAt.Format(time.ANSIC), troublemakerReport.Reason,
						reportedByUser.Username, reportedByUser.ID, reportedByGuild.Name, reportedByGuild.ID,
					)
				}

				for _, page := range helpers.Pagify(troublemakerText, "\n") {
					_, err := helpers.SendMessage(msg.ChannelID, page)
					helpers.Relax(err)
				}
			}
		default:
			helpers.RequireMod(msg, func() {
				session.ChannelTyping(msg.ChannelID)

				channel, err := helpers.GetChannel(msg.ChannelID)
				helpers.Relax(err)
				guild, err := helpers.GetGuild(channel.GuildID)
				helpers.Relax(err)

				if len(args) < 2 {
					helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
					return
				}

				targetUser, err := helpers.GetUserFromMention(args[0])
				if err != nil || targetUser.ID == "" {
					helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
					return
				}

				reasonText := strings.TrimSpace(strings.Replace(content, strings.Join(args[:1], " "), "", 1))

				if helpers.ConfirmEmbed(msg.ChannelID, msg.Author, helpers.GetTextF("plugins.troublemaker.report-confirm",
					targetUser.Username, targetUser.Discriminator, targetUser.ID, targetUser.ID, reasonText,
				), "✅", "🚫") == true {
					// Save to log DB
					troublemakerLogEntry := t.getEntryByOrCreateEmpty("id", "")
					troublemakerLogEntry.UserID = targetUser.ID
					troublemakerLogEntry.Reason = reasonText
					troublemakerLogEntry.CreatedAt = time.Now()
					troublemakerLogEntry.ReportedByGuildID = guild.ID
{'fraction_non_alphanumeric': 0.12362187922327335, 'fraction_numerical': 0.0021338644284799773, 'mean_word_length': 3.477707006369427, 'pattern_counts': {'":': 4, '<': 12, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7636207', 'n_tokens_mistral': 5870, 'n_tokens_neox': 4589, 'n_words': 710}