<commit_before>﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Converters;
using DSharpPlus.Entities;

namespace CompatBot.Commands.Converters
{
    internal sealed class CustomDiscordChannelConverter : IArgumentConverter<DiscordChannel>
    {
        private static Regex ChannelRegex { get; } = new Regex(@"^<#(\d+)>$", RegexOptions.ECMAScript | RegexOptions.Compiled);

        public async Task<Optional<DiscordChannel>> ConvertAsync(string value, CommandContext ctx)
        {
            var guildList = new List<DiscordGuild>(ctx.Client.Guilds.Count);
            if (ctx.Guild == null)
                foreach (var g in ctx.Client.Guilds.Keys)
                    guildList.Add(await ctx.Client.GetGuildAsync(g).ConfigureAwait(false));
            else
                guildList.Add(ctx.Guild);

            if (ulong.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var cid))
            {
                var result = (
                    from g in guildList
                    from ch in g.Channels
                    select ch
                ).FirstOrDefault(xc => xc.Id == cid);
                var ret = result == null ? Optional<DiscordChannel>.FromNoValue() : Optional<DiscordChannel>.FromValue(result);
                return ret;
            }

            var m = ChannelRegex.Match(value);
            if (m.Success && ulong.TryParse(m.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out cid))
            {
                var result = (
                    from g in guildList
                    from ch in g.Channels
                    select ch
                ).FirstOrDefault(xc => xc.Id == cid);
                var ret = result != null ? Optional<DiscordChannel>.FromValue(result) : Optional<DiscordChannel>.FromNoValue();
                return ret;
            }

            if (value.StartsWith('#'))
                value = value.Substring(1);
            value = value.ToLowerInvariant();
            var chn = (
                from g in guildList
                from ch in g.Channels
                select ch
            ).FirstOrDefault(xc => xc.Name.ToLowerInvariant() == value);
            return chn != null ? Optional<DiscordChannel>.FromValue(chn) : Optional<DiscordChannel>.FromNoValue();
        }
    }
}<commit_msg>do not look for the category channels, they're useless<commit_after>﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Converters;
using DSharpPlus.Entities;

namespace CompatBot.Commands.Converters
{
    internal sealed class CustomDiscordChannelConverter : IArgumentConverter<DiscordChannel>
    {
        private static Regex ChannelRegex { get; } = new Regex(@"^<#(\d+)>$", RegexOptions.ECMAScript | RegexOptions.Compiled);

        public async Task<Optional<DiscordChannel>> ConvertAsync(string value, CommandContext ctx)
        {
            var guildList = new List<DiscordGuild>(ctx.Client.Guilds.Count);
            if (ctx.Guild == null)
                foreach (var g in ctx.Client.Guilds.Keys)
                    guildList.Add(await ctx.Client.GetGuildAsync(g).ConfigureAwait(false));
            else
                guildList.Add(ctx.Guild);

            if (ulong.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var cid))
            {
                var result = (
                    from g in guildList
                    from ch in g.Channels
                    select ch
                ).FirstOrDefault(xc => xc.Id == cid && !xc.IsCategory);
                var ret = result == null ? Optional<DiscordChannel>.FromNoValue() : Optional<DiscordChannel>.FromValue(result);
                return ret;
            }

            var m = ChannelRegex.Match(value);
            if (m.Success && ulong.TryParse(m.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out cid))
            {
                var result = (
                    from g in guildList
                    from ch in g.Channels
                    select ch
                ).FirstOrDefault(xc => xc.Id == cid && !xc.IsCategory);
                var ret = result != null ? Optional<DiscordChannel>.FromValue(result) : Optional<DiscordChannel>.FromNoValue();
                return ret;
            }

            if (value.StartsWith('#'))
                value = value.Substring(1);
            value = value.ToLowerInvariant();
            var chn = (
                from g in guildList
                from ch in g.Channels
                select ch
            ).FirstOrDefault(xc => xc.Name.ToLowerInvariant() == value && !xc.IsCategory);
            return chn != null ? Optional<DiscordChannel>.FromValue(chn) : Optional<DiscordChannel>.FromNoValue();
        }
    }
}
{'fraction_non_alphanumeric': 0.09982174688057041, 'fraction_numerical': 0.0007922360863537334, 'mean_word_length': 1.9775943396226414, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 31, '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': '7588623', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1331, 'n_words': 299}