<commit_before>﻿using Discord;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Extensions;
using NadekoBot.Modules.Administration.Services;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace NadekoBot.Modules.Administration
{
    public partial class Administration
    {
        public class RoleCommands : NadekoSubmodule<RoleCommandsService>
        {
            public enum Exclude { Excl }

            public async Task InternalReactionRoles(bool exclusive, params string[] input)
            {
                var msgs = await ((SocketTextChannel)ctx.Channel).GetMessagesAsync().FlattenAsync().ConfigureAwait(false);
                var prev = (IUserMessage)msgs.FirstOrDefault(x => x is IUserMessage && x.Id != ctx.Message.Id);

                if (prev == null)
                    return;

                if (input.Length % 2 != 0)
                    return;

                var g = (SocketGuild)ctx.Guild;

                var grp = 0;
                var all = input
                    .GroupBy(x => grp++ / 2)
                    .Select(x =>
                    {
                        var inputRoleStr = x.First().ToLowerInvariant();
                        var role = g.Roles.FirstOrDefault(y => y.Name.ToLowerInvariant() == inputRoleStr);
                        if (role == null)
                        {
                            _log.Warn("Role {0} not found.", inputRoleStr);
                            return null;
                        }
                        //var emote = g.Emotes.FirstOrDefault(y => y.ToString() == x.Last());
                        //if (emote == null)
                        //{
                        //    _log.Warn("Emote {0} not found.", x.Last());
                        //    return null;
                        //}
                        //else

                        var emote = x.Last().ToIEmote();
                        return new { role, emote };
                    })
                    .Where(x => x != null);

                if (!all.Any())
                    return;

                foreach (var x in all)
                {
                    await prev.AddReactionAsync(x.emote, new RequestOptions()
                    {
                        RetryMode = RetryMode.Retry502 | RetryMode.RetryRatelimit
                    }).ConfigureAwait(false);
                    await Task.Delay(100).ConfigureAwait(false);
                }

                if (_service.Add(ctx.Guild.Id, new ReactionRoleMessage()
                {
                    Exclusive = exclusive,
                    MessageId = prev.Id,
                    ChannelId = prev.Channel.Id,
                    ReactionRoles = all.Select(x =>
                    {
                        return new ReactionRole()
                        {
                            EmoteName = x.emote.ToString(),
                            RoleId = x.role.Id,<commit_msg>Use a TypeReader for rero <commit_after>﻿using Discord;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Extensions;
using NadekoBot.Modules.Administration.Services;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace NadekoBot.Modules.Administration
{
    public partial class Administration
    {
        public class RoleCommands : NadekoSubmodule<RoleCommandsService>
        {
            private IServiceProvider _services;
            public enum Exclude { Excl }

            public RoleCommands(IServiceProvider services)
            {
                _services = services;
            }

            public async Task InternalReactionRoles(bool exclusive, params string[] input)
            {
                var msgs = await ((SocketTextChannel)ctx.Channel).GetMessagesAsync().FlattenAsync().ConfigureAwait(false);
                var prev = (IUserMessage)msgs.FirstOrDefault(x => x is IUserMessage && x.Id != ctx.Message.Id);

                if (prev == null)
                    return;

                if (input.Length % 2 != 0)
                    return;

                var g = (SocketGuild)ctx.Guild;

                var grp = 0;
                var results = input
                    .GroupBy(x => grp++ / 2)
                    .Select(async x =>
                   {
                       var inputRoleStr = x.First();
                       var roleReader = new RoleTypeReader<SocketRole>();
                       var roleResult = await roleReader.ReadAsync(ctx, inputRoleStr, _services);
                       if (!roleResult.IsSuccess)
                       {
                           _log.Warn("Role {0} not found.", inputRoleStr);
                           return null;
                       }
                       var role = (IRole)roleResult.BestMatch;
                       if (role.Position > ((IGuildUser)ctx.User).GetRoles().Select(r => r.Position).Max()
                           && ctx.User.Id != ctx.Guild.OwnerId)
                           return null;
                       var emote = x.Last().ToIEmote();
                       return new { role, emote };
                   })
                    .Where(x => x != null);

                var all = await Task.WhenAll(results);

                if (!all.Any())
                    return;

                foreach (var x in all)
                {
                    await prev.AddReactionAsync(x.emote, new RequestOptions()
                    {
                        RetryMode = RetryMode.Retry502 | RetryMode.RetryRatelimit
                    }).ConfigureAwait(false);
                    await Task.Delay(100).ConfigureAwait(false);
                }

                if (_service.Add(ctx.Guild.Id, new ReactionRoleMessage()
                {
                    Exclusive = exclusive,
                    MessageId = prev.Id,
                    ChannelId = prev.Channel.Id,
                    ReactionRoles = all.Select(x =>
                    {
                        return new ReactionRole()
                        {
                            EmoteName = x.emote.ToString(),
                            RoleId = x.role.Id,
{'fraction_non_alphanumeric': 0.09147383134027023, 'fraction_numerical': 0.003571983227209194, 'mean_word_length': 1.2085048010973938, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 20, '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': '3134223', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1551, 'n_words': 338}