<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CompatBot.Commands.Attributes;
using CompatBot.Database;
using CompatBot.EventHandlers;
using CompatBot.Utils;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using Microsoft.EntityFrameworkCore;

namespace CompatBot.Commands
{
    [Group("rename"), RequiresBotModRole]
    [Description("Manage users who has forced nickname.")]
    internal sealed class ForcedNicknames : BaseCommandModuleCustom
    {
        [GroupCommand]
        [Description("Enforces specific nickname for particular user.")]
        public async Task Add(CommandContext ctx, 
            [Description("Discord user to add to forced nickname list.")] DiscordUser discordUser, 
            [Description("Nickname which should be displayed."), RemainingText] string expectedNickname)
        {
            try
            {
                if (expectedNickname.Length < 2 || expectedNickname.Length > 32)
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure, "Nickname must be between 2 and 32 characters long", true).ConfigureAwait(false);
                    return;
                }

                if (!expectedNickname.All(c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c)))
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure, "Nickname must follow Rule 7", true).ConfigureAwait(false);
                    return;
                }

                List<DiscordGuild> guilds;
                if (ctx.Guild == null)
                {
                    guilds = ctx.Client.Guilds?.Values.ToList() ?? new List<DiscordGuild>(0);
                    if (guilds.Count > 1)
                        await ctx.RespondAsync($"{discordUser.Mention} will be renamed in all {guilds.Count} servers").ConfigureAwait(false);
                }
                else
                    guilds = new List<DiscordGuild> {ctx.Guild};

                int changed = 0, noPermissions = 0, skipped = 0, failed = 0;
                using var db = new BotDb();
                foreach (var guild in guilds)
                {
                    var enforceRules = db.ForcedNicknames.FirstOrDefault(mem => mem.UserId == discordUser.Id && mem.GuildId == guild.Id);
                    if (enforceRules is null)
                    {
                        enforceRules = new ForcedNickname {UserId = discordUser.Id, GuildId = guild.Id, Nickname = expectedNickname};
                        db.ForcedNicknames.Add(enforceRules);
                    }
                    else
                    {
                        if (enforceRules.Nickname == expectedNickname)
                        {
                            skipped++;
                            continue;
                        }
                        enforceRules.Nickname = expectedNickname;
                    }
                    if (!(ctx.Guild?.Permissions?.HasFlag(Permissions.ChangeNickname) ?? true))
                    {
                        noPermissions++;
                        continue;
                    }

                    if (ctx.Client.GetMember(guild, discordUser) is DiscordMember discordMember)
                        try
                        {
                            await discordMember.ModifyAsync(x => x.Nickname = expectedNickname).ConfigureAwait(false);
                            changed++;
                        }
                        catch (Exception ex)
                        {
                            Config.Log.Warn(ex, "Failed to change nickname");
                            failed++;
                        }
                }
                await db.SaveChangesAsync().ConfigureAwait(false);
                if (guilds.Count > 1)
                {
                    if (changed > 0)
                        await ctx.ReactWithAsync(Config.Reactions.Success, $"Forced nickname for {discordUser.Mention} in {changed} server{(changed == 1 ? "" : "s")}", true).ConfigureAwait(false);
                    else
                        await ctx.ReactWithAsync(Config.Reactions.Failure, $"Failed to force nickname for {discordUser.Mention} in any server").ConfigureAwait(false);
                }
                else
                {
                    if (changed > 0)
                        await ctx.ReactWithAsync(Config.Reactions.Success, $"Forced nickname for {discordUser.Mention}", true).ConfigureAwait(false);
                    else if (failed > 0)
                        await ctx.ReactWithAsync(Config.Reactions.Failure, $"Failed to force nickname for {discordUser.Mention}").ConfigureAwait(false);
                    else if (noPermissions > 0)
                        await ctx.ReactWithAsync(Config.Reactions.Failure, $"No permissions to force nickname for {discordUser.Mention}").ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                Config.Log.Error(e);
                await ctx.ReactWithAsync(Config.Reactions.Failure, "Failed to change nickname, check bot's permissions").ConfigureAwait(false);
            }
        }

        [Command("clear"), Aliases("remove")]
        [Description("Removes nickname restriction from particular user.")]
        public async Task Remove(CommandContext ctx, [Description("Discord user to remove from forced nickname list.")] DiscordUser discordUser)
        {
            try
            {
                using var db = new BotDb();
                var enforcedRules = ctx.Guild == null
                    ? await db.ForcedNicknames.Where(mem => mem.UserId == discordUser.Id).ToListAsync().ConfigureAwait(false)
                    : await db.ForcedNicknames.Where(mem => mem.UserId == discordUser.Id && mem.GuildId == ctx.Guild.Id).ToListAsync().ConfigureAwait(false);
                if (enforcedRules.Count == 0)
                    return;

                db.ForcedNicknames.RemoveRange(enforcedRules);
                await db.SaveChangesAsync().ConfigureAwait(false);
                foreach (var rule in enforcedRules)
                    if (ctx.Client.GetMember(rule.GuildId, discordUser) is DiscordMember discordMember)
                        try
                        {
                            //todo: change to mem.Nickname = default when the library fixes their shit
                            await discordMember.ModifyAsync(mem => mem.Nickname = new Optional<string>("")).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            Config.Log.Debug(ex);
                        }
                await ctx.ReactWithAsync(Config.Reactions.Success).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Config.Log.Error(e);
                await ctx.ReactWithAsync(Config.Reactions.Failure, "Failed to reset user nickname").ConfigureAwait(false);
            }
        }

        [Command("cleanup"), Aliases("fix")]
        [Description("Removes zalgo from specified user nickname")]
        public async Task Cleanup(CommandContext ctx, [Description("Discord user to clean up")] DiscordMember discordUser)
        {
            var name = discordUser.DisplayName;
            var newName = UsernameZalgoMonitor.StripZalgo(name, discordUser.Id);
            if (name != newName)
                try
                {
                    await ctx.Client.UpdateCurrentUserAsync(username: newName).ConfigureAwait(false);
                    await ctx.ReactWithAsync(Config.Reactions.Success, $"Renamed user to {newName}", true).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    Config.Log.Warn($"Failed to rename user {discordUser.Username}#{discordUser.Discriminator}");
                    await ctx.ReactWithAsync(Config.Reactions.Failure, $"Failed to rename user to {newName}").ConfigureAwait(false);
                }
        }
        
        [Command("list")]
        [Description("Lists all users who has restricted nickname.")]
        public async Task List(CommandContext ctx)
        {
            using var db = new BotDb();
            var selectExpr = db.ForcedNicknames.AsNoTracking();
            if (ctx.Guild != null)
                selectExpr = selectExpr.Where(mem => mem.GuildId == ctx.Guild.Id);

            var forcedNicknames = (
                from m in selectExpr.AsEnumerable()
                orderby m.UserId, m.Nickname
                let result = new {m.UserId, m.Nickname}
                select result
            ).ToList();
            if (forcedNicknames.Count == 0)
            {
                await ctx.RespondAsync("No users with forced nicknames").ConfigureAwait(false);<commit_msg>add explicit message when no zalgo was removed<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CompatBot.Commands.Attributes;
using CompatBot.Database;
using CompatBot.EventHandlers;
using CompatBot.Utils;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using Microsoft.EntityFrameworkCore;

namespace CompatBot.Commands
{
    [Group("rename"), RequiresBotModRole]
    [Description("Manage users who has forced nickname.")]
    internal sealed class ForcedNicknames : BaseCommandModuleCustom
    {
        [GroupCommand]
        [Description("Enforces specific nickname for particular user.")]
        public async Task Add(CommandContext ctx, 
            [Description("Discord user to add to forced nickname list.")] DiscordUser discordUser, 
            [Description("Nickname which should be displayed."), RemainingText] string expectedNickname)
        {
            try
            {
                if (expectedNickname.Length < 2 || expectedNickname.Length > 32)
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure, "Nickname must be between 2 and 32 characters long", true).ConfigureAwait(false);
                    return;
                }

                if (!expectedNickname.All(c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c)))
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure, "Nickname must follow Rule 7", true).ConfigureAwait(false);
                    return;
                }

                List<DiscordGuild> guilds;
                if (ctx.Guild == null)
                {
                    guilds = ctx.Client.Guilds?.Values.ToList() ?? new List<DiscordGuild>(0);
                    if (guilds.Count > 1)
                        await ctx.RespondAsync($"{discordUser.Mention} will be renamed in all {guilds.Count} servers").ConfigureAwait(false);
                }
                else
                    guilds = new List<DiscordGuild> {ctx.Guild};

                int changed = 0, noPermissions = 0, skipped = 0, failed = 0;
                using var db = new BotDb();
                foreach (var guild in guilds)
                {
                    var enforceRules = db.ForcedNicknames.FirstOrDefault(mem => mem.UserId == discordUser.Id && mem.GuildId == guild.Id);
                    if (enforceRules is null)
                    {
                        enforceRules = new ForcedNickname {UserId = discordUser.Id, GuildId = guild.Id, Nickname = expectedNickname};
                        db.ForcedNicknames.Add(enforceRules);
                    }
                    else
                    {
                        if (enforceRules.Nickname == expectedNickname)
                        {
                            skipped++;
                            continue;
                        }
                        enforceRules.Nickname = expectedNickname;
                    }
                    if (!(ctx.Guild?.Permissions?.HasFlag(Permissions.ChangeNickname) ?? true))
                    {
                        noPermissions++;
                        continue;
                    }

                    if (ctx.Client.GetMember(guild, discordUser) is DiscordMember discordMember)
                        try
                        {
                            await discordMember.ModifyAsync(x => x.Nickname = expectedNickname).ConfigureAwait(false);
                            changed++;
                        }
                        catch (Exception ex)
                        {
                            Config.Log.Warn(ex, "Failed to change nickname");
                            failed++;
                        }
                }
                await db.SaveChangesAsync().ConfigureAwait(false);
                if (guilds.Count > 1)
                {
                    if (changed > 0)
                        await ctx.ReactWithAsync(Config.Reactions.Success, $"Forced nickname for {discordUser.Mention} in {changed} server{(changed == 1 ? "" : "s")}", true).ConfigureAwait(false);
                    else
                        await ctx.ReactWithAsync(Config.Reactions.Failure, $"Failed to force nickname for {discordUser.Mention} in any server").ConfigureAwait(false);
                }
                else
                {
                    if (changed > 0)
                        await ctx.ReactWithAsync(Config.Reactions.Success, $"Forced nickname for {discordUser.Mention}", true).ConfigureAwait(false);
                    else if (failed > 0)
                        await ctx.ReactWithAsync(Config.Reactions.Failure, $"Failed to force nickname for {discordUser.Mention}").ConfigureAwait(false);
                    else if (noPermissions > 0)
                        await ctx.ReactWithAsync(Config.Reactions.Failure, $"No permissions to force nickname for {discordUser.Mention}").ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                Config.Log.Error(e);
                await ctx.ReactWithAsync(Config.Reactions.Failure, "Failed to change nickname, check bot's permissions").ConfigureAwait(false);
            }
        }

        [Command("clear"), Aliases("remove")]
        [Description("Removes nickname restriction from particular user.")]
        public async Task Remove(CommandContext ctx, [Description("Discord user to remove from forced nickname list.")] DiscordUser discordUser)
        {
            try
            {
                using var db = new BotDb();
                var enforcedRules = ctx.Guild == null
                    ? await db.ForcedNicknames.Where(mem => mem.UserId == discordUser.Id).ToListAsync().ConfigureAwait(false)
                    : await db.ForcedNicknames.Where(mem => mem.UserId == discordUser.Id && mem.GuildId == ctx.Guild.Id).ToListAsync().ConfigureAwait(false);
                if (enforcedRules.Count == 0)
                    return;

                db.ForcedNicknames.RemoveRange(enforcedRules);
                await db.SaveChangesAsync().ConfigureAwait(false);
                foreach (var rule in enforcedRules)
                    if (ctx.Client.GetMember(rule.GuildId, discordUser) is DiscordMember discordMember)
                        try
                        {
                            //todo: change to mem.Nickname = default when the library fixes their shit
                            await discordMember.ModifyAsync(mem => mem.Nickname = new Optional<string>("")).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            Config.Log.Debug(ex);
                        }
                await ctx.ReactWithAsync(Config.Reactions.Success).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Config.Log.Error(e);
                await ctx.ReactWithAsync(Config.Reactions.Failure, "Failed to reset user nickname").ConfigureAwait(false);
            }
        }

        [Command("cleanup"), Aliases("fix")]
        [Description("Removes zalgo from specified user nickname")]
        public async Task Cleanup(CommandContext ctx, [Description("Discord user to clean up")] DiscordMember discordUser)
        {
            var name = discordUser.DisplayName;
            var newName = UsernameZalgoMonitor.StripZalgo(name, discordUser.Id);
            if (name == newName)
            {
                await ctx.RespondAsync("Failed to remove any extra symbols").ConfigureAwait(false);
            }
            else
            {
                try
                {
                    await ctx.Client.UpdateCurrentUserAsync(username: newName).ConfigureAwait(false);
                    await ctx.ReactWithAsync(Config.Reactions.Success, $"Renamed user to {newName}", true).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    Config.Log.Warn($"Failed to rename user {discordUser.Username}#{discordUser.Discriminator}");
                    await ctx.ReactWithAsync(Config.Reactions.Failure, $"Failed to rename user to {newName}").ConfigureAwait(false);
                }
            }
        }
        
        [Command("list")]
        [Description("Lists all users who has restricted nickname.")]
        public async Task List(CommandContext ctx)
        {
            using var db = new BotDb();
            var selectExpr = db.ForcedNicknames.AsNoTracking();
            if (ctx.Guild != null)
                selectExpr = selectExpr.Where(mem => mem.GuildId == ctx.Guild.Id);

            var forcedNicknames = (
                from m in selectExpr.AsEnumerable()
                orderby m.UserId, m.Nickname
                let result = new {m.UserId, m.Nickname}
                select result
            ).ToList();
            if (forcedNicknames.Count == 0)
            {
                await ctx.RespondAsync("No users with forced nicknames").ConfigureAwait(false);
{'fraction_non_alphanumeric': 0.09124612660469235, 'fraction_numerical': 0.0023240371845949536, 'mean_word_length': 1.5505221563646627, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 39, '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': '4423984', 'n_tokens_mistral': 4665, 'n_tokens_neox': 4344, 'n_words': 994}