<commit_before>using Discord.Commands;
using NadekoBot.Commands;
using NadekoBot.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Threading.Tasks;

namespace NadekoBot.Modules.Gambling
{
    internal class DiceRollCommand : DiscordCommand
    {

        public DiceRollCommand(DiscordModule module) : base(module) { }


        internal override void Init(CommandGroupBuilder cgb)
        {
            cgb.CreateCommand(Module.Prefix + "roll")
                .Description("Rolls 0-100. If you supply a number [x] it rolls up to 30 normal dice.\n**Usage**: $roll [x]")
                .Parameter("num", ParameterType.Optional)
                .Do(Roll0to10Func());
            cgb.CreateCommand(Module.Prefix + "nroll")
                .Description("Rolls in a given range.\n**Usage**: `$nroll 5` (rolls 0-5) or `$nroll 5-15`")
                .Parameter("range", ParameterType.Required)
                .Do(Roll0to5Func());
        }

        private Image GetDice(int num) => num != 10
                                          ? Properties.Resources.ResourceManager.GetObject("_" + num) as Image
                                          : new[]
                                            {
                                              (Properties.Resources.ResourceManager.GetObject("_" + 1) as Image),
                                              (Properties.Resources.ResourceManager.GetObject("_" + 0) as Image),
                                            }.Merge();

        private Func<CommandEventArgs, Task> Roll0to10Func()
        {
            var r = new Random();
            return async e =>
            {
                if (e.Args[0] == "")
                {
                    var gen = r.Next(0, 101);

                    var num1 = gen / 10;
                    var num2 = gen % 10;

                    var imageStream = new Image[2] { GetDice(num1), GetDice(num2) }.Merge().ToStream(ImageFormat.Png);

                    await e.Channel.SendFile("dice.png", imageStream);
                }
                else
                {
                    try
                    {
                        var num = int.Parse(e.Args[0]);
                        if (num < 1) num = 1;
                        if (num > 30)
                        {
                            await e.Channel.SendMessage("You can roll up to 30 dice at a time.");
                            num = 30;
                        }
                        var dices = new List<Image>(num);
                        var values = new List<int>(num);
                        for (var i = 0; i < num; i++)
                        {
                            var randomNumber = r.Next(1, 7);
                            var toInsert = dices.Count;
                            if (randomNumber == 6 || dices.Count == 0)
                                toInsert = 0;
                            else if (randomNumber != 1)
                                for (var j = 0; j < dices.Count; j++)
                                {
                                    if (values[j] < randomNumber)
                                    {
                                        toInsert = j;
                                        break;
                                    }
                                }
                            dices.Insert(toInsert, GetDice(randomNumber));
                            values.Insert(toInsert, randomNumber);
                        }

                        var bitmap = dices.Merge();
                        await e.Channel.SendMessage(values.Count + " Dice rolled. Total: **" + values.Sum() + "** Average: **" + (values.Sum() / (1.0f * values.Count)).ToString("N2") + "**");
                        await e.Channel.SendFile("dice.png", bitmap.ToStream(ImageFormat.Png));
                    }
                    catch
                    {
                        await e.Channel.SendMessage("Please enter a number of dice to roll.");
                    }
                }
            };
        }


        private Func<CommandEventArgs, Task> Roll0to5Func() =>
            async e =>
            {
                try
                {
                    int rolled;
                    if (e.GetArg("range").Contains("-"))
                    {
                        var arr = e.GetArg("range").Split('-')<commit_msg>dnd style $roll (for example: 3d5)<commit_after>using Discord.Commands;
using NadekoBot.Commands;
using NadekoBot.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace NadekoBot.Modules.Gambling
{
    internal class DiceRollCommand : DiscordCommand
    {

        public DiceRollCommand(DiscordModule module) : base(module) { }


        internal override void Init(CommandGroupBuilder cgb)
        {
            cgb.CreateCommand(Module.Prefix + "roll")
                .Description("Rolls 0-100. If you supply a number [x] it rolls up to 30 normal dice." +
                             " If you split 2 numbers with letter d (xdy) it will roll x dice from 1 to y.\n**Usage**: $roll or $roll 7 or $roll 3d5")
                .Parameter("num", ParameterType.Optional)
                .Do(RollFunc());
            cgb.CreateCommand(Module.Prefix + "nroll")
                .Description("Rolls in a given range.\n**Usage**: `$nroll 5` (rolls 0-5) or `$nroll 5-15`")
                .Parameter("range", ParameterType.Required)
                .Do(NRollFunc());
        }

        private Image GetDice(int num) => num != 10
                                          ? Properties.Resources.ResourceManager.GetObject("_" + num) as Image
                                          : new[]
                                            {
                                              (Properties.Resources.ResourceManager.GetObject("_" + 1) as Image),
                                              (Properties.Resources.ResourceManager.GetObject("_" + 0) as Image),
                                            }.Merge();


        Regex dndRegex = new Regex(@"(?<n1>\d+)d(?<n2>\d+)", RegexOptions.Compiled);
        private Func<CommandEventArgs, Task> RollFunc()
        {
            var r = new Random();
            return async e =>
            {
                var arg = e.Args[0]?.Trim();
                if (string.IsNullOrWhiteSpace(arg))
                {
                    var gen = r.Next(0, 101);

                    var num1 = gen / 10;
                    var num2 = gen % 10;

                    var imageStream = new Image[2] { GetDice(num1), GetDice(num2) }.Merge().ToStream(ImageFormat.Png);

                    await e.Channel.SendFile("dice.png", imageStream);
                    return;
                }
                Match m;
                if ((m = dndRegex.Match(arg)).Length != 0)
                {
                    int n1;
                    int n2;
                    if (int.TryParse(m.Groups["n1"].ToString(), out n1) &&
                        int.TryParse(m.Groups["n2"].ToString(), out n2) &&
                        n1 <= 50 && n2 <= 100000 && n1 > 0 && n2 > 0)
                    {
                        var arr = new int[n1];
                        for (int i = 0; i < n1; i++)
                        {
                            arr[i] = r.Next(1, n2 + 1);
                        }
                        await e.Channel.SendMessage($"`Rolled {n1} {(n1 == 1 ? "die" : "dice")} 1-{n2}.`\n`Result:`" + string.Join(", ", arr.OrderBy(x => x)));
                    }
                }
                try
                {
                    var num = int.Parse(e.Args[0]);
                    if (num < 1) num = 1;
                    if (num > 30)
                    {
                        await e.Channel.SendMessage("You can roll up to 30 dice at a time.");
                        num = 30;
                    }
                    var dices = new List<Image>(num);
                    var values = new List<int>(num);
                    for (var i = 0; i < num; i++)
                    {
                        var randomNumber = r.Next(1, 7);
                        var toInsert = dices.Count;
                        if (randomNumber == 6 || dices.Count == 0)
                            toInsert = 0;
                        else if (randomNumber != 1)
                            for (var j = 0; j < dices.Count; j++)
                            {
                                if (values[j] < randomNumber)
                                {
                                    toInsert = j;
                                    break;
                                }
                            }
                        dices.Insert(toInsert, GetDice(randomNumber));
                        values.Insert(toInsert, randomNumber);
                    }

                    var bitmap = dices.Merge();
                    await e.Channel.SendMessage(values.Count + " Dice rolled. Total: **" + values.Sum() + "** Average: **" + (values.Sum() / (1.0f * values.Count)).ToString("N2") + "**");
                    await e.Channel.SendFile("dice.png", bitmap.ToStream(ImageFormat.Png));
                }
                catch
                {
                    await e.Channel.SendMessage("Please enter a number of dice to roll.");
                }
            };
        }


        private Func<CommandEventArgs, Task> NRollFunc() =>
            async e =>
            {
                try
                {
                    int rolled;
                    if (e.GetArg("range").Contains("-"))
                    {
                        var arr = e.GetArg("range").Split('-')
{'fraction_non_alphanumeric': 0.1094003241491086, 'fraction_numerical': 0.01529578606158833, 'mean_word_length': 1.0034496753246753, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6977504', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2348, 'n_words': 574}