mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-04 00:34:26 -05:00 
			
		
		
		
	Using new .Chunk and .DistinctBy Linq Extensions
This commit is contained in:
		@@ -30,7 +30,7 @@ public partial class Administration
 | 
			
		||||
 | 
			
		||||
            var grp = 0;
 | 
			
		||||
            var results = input
 | 
			
		||||
                .GroupBy(x => grp++ / 2)
 | 
			
		||||
                .Chunk(input.Length / 2)
 | 
			
		||||
                .Select(async x =>
 | 
			
		||||
                {
 | 
			
		||||
                    var inputRoleStr = x.First();
 | 
			
		||||
 
 | 
			
		||||
@@ -47,8 +47,7 @@ public class PruneService : INService
 | 
			
		||||
                if (bulkDeletable.Count > 0)
 | 
			
		||||
                    await Task.WhenAll(Task.Delay(1000), channel.DeleteMessagesAsync(bulkDeletable)).ConfigureAwait(false);
 | 
			
		||||
 | 
			
		||||
                var i = 0;
 | 
			
		||||
                foreach (var group in singleDeletable.GroupBy(x => ++i / (singleDeletable.Count / 5)))
 | 
			
		||||
                foreach (var group in singleDeletable.Chunk(5))
 | 
			
		||||
                    await Task.WhenAll(Task.Delay(1000), Task.WhenAll(group.Select(x => x.DeleteAsync()))).ConfigureAwait(false);
 | 
			
		||||
 | 
			
		||||
                //this isn't good, because this still work as if i want to remove only specific user's messages from the last
 | 
			
		||||
 
 | 
			
		||||
@@ -159,9 +159,6 @@ public class Help : NadekoModule<HelpService>
 | 
			
		||||
    [NadekoOptions(typeof(CommandsOptions))]
 | 
			
		||||
    public async Task Commands(string module = null, params string[] args)
 | 
			
		||||
    {
 | 
			
		||||
        var channel = ctx.Channel;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        module = module?.Trim().ToUpperInvariant();
 | 
			
		||||
        if (string.IsNullOrWhiteSpace(module))
 | 
			
		||||
        {
 | 
			
		||||
@@ -177,7 +174,7 @@ public class Help : NadekoModule<HelpService>
 | 
			
		||||
        var cmds = _cmds.Commands.Where(c => c.Module.GetTopLevelModule().Name.ToUpperInvariant().StartsWith(module, StringComparison.InvariantCulture))
 | 
			
		||||
            .Where(c => !_perms.BlockedCommands.Contains(c.Aliases[0].ToLowerInvariant()))
 | 
			
		||||
            .OrderBy(c => c.Aliases[0])
 | 
			
		||||
            .Distinct(new CommandTextEqualityComparer());
 | 
			
		||||
            .DistinctBy(x => x.Aliases[0]);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        // check preconditions for all commands, but only if it's not 'all'
 | 
			
		||||
@@ -199,11 +196,11 @@ public class Help : NadekoModule<HelpService>
 | 
			
		||||
                cmds = cmds.Where(x => succ.Contains(x));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        var cmdsWithGroup = cmds.GroupBy(c => c.Module.Name.Replace("Commands", "", StringComparison.InvariantCulture))
 | 
			
		||||
            .OrderBy(x => x.Key == x.First().Module.Name ? int.MaxValue : x.Count());
 | 
			
		||||
            .OrderBy(x => x.Key == x.First().Module.Name ? int.MaxValue : x.Count())
 | 
			
		||||
            .ToList();
 | 
			
		||||
 | 
			
		||||
        if (!cmds.Any())
 | 
			
		||||
        if (cmdsWithGroup.Count == 0)
 | 
			
		||||
        {
 | 
			
		||||
            if (opts.View != CommandsOptions.ViewType.Hide)
 | 
			
		||||
                await ReplyErrorLocalizedAsync(strs.module_not_found).ConfigureAwait(false);
 | 
			
		||||
@@ -211,13 +208,14 @@ public class Help : NadekoModule<HelpService>
 | 
			
		||||
                await ReplyErrorLocalizedAsync(strs.module_not_found_or_cant_exec).ConfigureAwait(false);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        var i = 0;
 | 
			
		||||
        var groups = cmdsWithGroup.GroupBy(x => i++ / 48).ToArray();
 | 
			
		||||
 | 
			
		||||
        var cnt = 0;
 | 
			
		||||
        var groups = cmdsWithGroup.GroupBy(x => cnt++ / 48).ToArray();
 | 
			
		||||
        var embed = _eb.Create().WithOkColor();
 | 
			
		||||
        foreach (var g in groups)
 | 
			
		||||
        {
 | 
			
		||||
            var last = g.Count();
 | 
			
		||||
            for (i = 0; i < last; i++)
 | 
			
		||||
            for (var i = 0; i < last; i++)
 | 
			
		||||
            {
 | 
			
		||||
                var transformed = g.ElementAt(i).Select(x =>
 | 
			
		||||
                {
 | 
			
		||||
@@ -231,10 +229,8 @@ public class Help : NadekoModule<HelpService>
 | 
			
		||||
 | 
			
		||||
                if (i == last - 1 && (i + 1) % 2 != 0)
 | 
			
		||||
                {
 | 
			
		||||
                    var grp = 0;
 | 
			
		||||
                    var count = transformed.Count();
 | 
			
		||||
                    transformed = transformed
 | 
			
		||||
                        .GroupBy(x => grp++ % count / 2)
 | 
			
		||||
                        .Chunk(2)
 | 
			
		||||
                        .Select(x =>
 | 
			
		||||
                        {
 | 
			
		||||
                            if (x.Count() == 1)
 | 
			
		||||
@@ -308,7 +304,7 @@ public class Help : NadekoModule<HelpService>
 | 
			
		||||
            .OrderBy(x => x.Key)
 | 
			
		||||
            .ToDictionary(
 | 
			
		||||
                x => x.Key,
 | 
			
		||||
                x => x.Distinct(c => c.Aliases.First())
 | 
			
		||||
                x => x.DistinctBy(c => c.Aliases.First())
 | 
			
		||||
                    .Select(com =>
 | 
			
		||||
                    {
 | 
			
		||||
                        List<string> optHelpStr = null;
 | 
			
		||||
@@ -423,14 +419,6 @@ public class Help : NadekoModule<HelpService>
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
public class CommandTextEqualityComparer : IEqualityComparer<CommandInfo>
 | 
			
		||||
{
 | 
			
		||||
    public bool Equals(CommandInfo x, CommandInfo y) => x.Aliases[0] == y.Aliases[0];
 | 
			
		||||
 | 
			
		||||
    public int GetHashCode(CommandInfo obj) => obj.Aliases[0].GetHashCode(StringComparison.InvariantCulture);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal class CommandJsonObject
 | 
			
		||||
{
 | 
			
		||||
    public string[] Aliases { get; set; }
 | 
			
		||||
 
 | 
			
		||||
@@ -39,7 +39,7 @@ public partial class Utility
 | 
			
		||||
        {
 | 
			
		||||
            var selection = typeof(Math).GetTypeInfo()
 | 
			
		||||
                .GetMethods()
 | 
			
		||||
                .Distinct(new MethodInfoEqualityComparer())
 | 
			
		||||
                .DistinctBy(x => x.Name)
 | 
			
		||||
                .Select(x => x.Name)
 | 
			
		||||
                .Except(new[]
 | 
			
		||||
                {
 | 
			
		||||
@@ -51,11 +51,4 @@ public partial class Utility
 | 
			
		||||
            await SendConfirmAsync(GetText(strs.calcops(Prefix)), string.Join(", ", selection));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private class MethodInfoEqualityComparer : IEqualityComparer<MethodInfo>
 | 
			
		||||
    {
 | 
			
		||||
        public bool Equals(MethodInfo x, MethodInfo y) => x.Name == y.Name;
 | 
			
		||||
 | 
			
		||||
        public int GetHashCode(MethodInfo obj) => obj.Name.GetHashCode(StringComparison.InvariantCulture);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -30,7 +30,7 @@ public class CommandMapService : IInputTransformer, INService
 | 
			
		||||
                .ToDictionary(
 | 
			
		||||
                    x => x.GuildId,
 | 
			
		||||
                    x => new ConcurrentDictionary<string, string>(x.CommandAliases
 | 
			
		||||
                        .Distinct(new CommandAliasEqualityComparer())
 | 
			
		||||
                        .DistinctBy(ca => ca.Trigger)
 | 
			
		||||
                        .ToDictionary(ca => ca.Trigger, ca => ca.Mapping))));
 | 
			
		||||
 | 
			
		||||
            _db = db;
 | 
			
		||||
@@ -96,11 +96,4 @@ public class CommandMapService : IInputTransformer, INService
 | 
			
		||||
 | 
			
		||||
        return input;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
public class CommandAliasEqualityComparer : IEqualityComparer<CommandAlias>
 | 
			
		||||
{
 | 
			
		||||
    public bool Equals(CommandAlias x, CommandAlias y) => x.Trigger == y.Trigger;
 | 
			
		||||
 | 
			
		||||
    public int GetHashCode(CommandAlias obj) => obj.Trigger.GetHashCode(StringComparison.InvariantCulture);
 | 
			
		||||
}
 | 
			
		||||
@@ -20,10 +20,6 @@ public static class IEnumerableExtensions
 | 
			
		||||
 | 
			
		||||
        return string.Join(separator, data.Select(func));
 | 
			
		||||
    }
 | 
			
		||||
        
 | 
			
		||||
    public static IEnumerable<T> Distinct<T, U>(this IEnumerable<T> data, Func<T, U> getKey) =>
 | 
			
		||||
        data.GroupBy(x => getKey(x))
 | 
			
		||||
            .Select(x => x.First());
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Randomize element order by performing the Fisher-Yates shuffle
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user