<commit_before>﻿using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NadekoBot.Services;
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
using ImageSharp;
using System.IO;
using System.Threading;

namespace NadekoBot.Modules.Gambling
{
    [NadekoModule("Gambling", "$")]
    public partial class Gambling : DiscordModule
    {
        public static string CurrencyName { get; set; }
        public static string CurrencyPluralName { get; set; }
        public static string CurrencySign { get; set; }

        public static string coloredNamesFile = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "ColoredNames.txt");

        static Gambling()
        {
            using (var uow = DbHandler.UnitOfWork())
            {
                var conf = uow.BotConfig.GetOrCreate();

                CurrencyName = conf.CurrencyName;
                CurrencySign = conf.CurrencySign;
                CurrencyPluralName = conf.CurrencyPluralName;
            }

            var nameColorTimer = new Timer(async (e) =>
            {
                await CheckForExpiredColors();
            }, null, 0, System.Convert.ToInt32(System.TimeSpan.FromMinutes(1).TotalMilliseconds));
        }

        public static async Task CheckForExpiredColors()
        {
            while (true)
            {
                if (System.IO.File.Exists(coloredNamesFile))
                {
                    int i = 0;
                    List<string> lines = File.ReadLines(coloredNamesFile).ToList();
                    lines = lines.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
                    List<int> linesToRemove = new List<int>();
                    foreach (string line in lines)
                    {
                        if (line == null)
                        {
                            return;
                        }

                        System.String[] substrings = line.Split(',');
                        string userId = substrings[0];
                        System.DateTime expireDate = System.Convert.ToDateTime(substrings[1]);
                        if (expireDate < System.DateTime.Now)
                        {
                            try
                            {
                                IGuild guild = NadekoBot.Client.GetGuilds().First();
                                IGuildUser user = NadekoBot.Client.GetGuilds().First().GetUser(System.Convert.ToUInt64(userId));
                                if (user != null)
                                {
                                    await user.SendMessageAsync("Whoops! Looks like your colored name has just **expired**! Sorry about that! You can renew your colored name by buying it again at any time in the shop. (e.g. $shop namecolor red, or $shop namecolor #ff0000)");
                                    await clearColors(guild, user);
                                }

                                linesToRemove.Add(i);
                            }
                            catch (System.Exception e)
                            {
                                System.Console.WriteLine("Something went wrong in CheckForExpiredColors method.\n\nError:\n" + e);
                            }
                        }
                        i++;
                    }
                    if (linesToRemove.Count > 0)
                    {
                        int x = 0;
                        foreach (int lineNumber in linesToRemove)
                        {
                            lines.RemoveAt(lineNumber - x);
                            x++;
                        }
                        File.WriteAllLines(coloredNamesFile, lines);
                    }
                }
                await Task.Delay(System.DateTime.Now.AddMinutes(1).Millisecond);
            }
        }

        public static long GetCurrency(ulong id)
        {
            using (var uow = DbHandler.UnitOfWork())
            {
                return uow.Currency.GetUserCurrency(id);
            }
        }

        bool ColorsAreClose(ImageSharp.Color a, ImageSharp.Color z, int threshold = 50)
        {
            int r = (int)a.R - z.R,
                g = (int)a.G - z.G,
                b = (int)a.B - z.B;
            return (r * r + g * g + b * b) <= threshold * threshold;
        }

        [NadekoCommand, Usage, Description, Aliases]
        [RequireContext(ContextType.Guild)]
        public async Task Raffle([Remainder] IRole role = null)
        {
            role = role ?? Context.Guild.EveryoneRole;

            var members = role.Members().Where(u => u.Status != UserStatus.Offline && u.Status != UserStatus.Unknown);<commit_msg>colored name task tweak to repeat interval<commit_after>﻿using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NadekoBot.Services;
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
using ImageSharp;
using System.IO;
using System.Threading;

namespace NadekoBot.Modules.Gambling
{
    [NadekoModule("Gambling", "$")]
    public partial class Gambling : DiscordModule
    {
        public static string CurrencyName { get; set; }
        public static string CurrencyPluralName { get; set; }
        public static string CurrencySign { get; set; }

        public static string coloredNamesFile = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "ColoredNames.txt");

        static Gambling()
        {
            using (var uow = DbHandler.UnitOfWork())
            {
                var conf = uow.BotConfig.GetOrCreate();

                CurrencyName = conf.CurrencyName;
                CurrencySign = conf.CurrencySign;
                CurrencyPluralName = conf.CurrencyPluralName;
            }

            var nameColorTimer = new Timer(async (e) =>
            {
                await CheckForExpiredColors();
            }, null, 0, System.Convert.ToInt32(System.TimeSpan.FromHours(12).TotalMilliseconds));
        }

        public static async Task CheckForExpiredColors()
        {
            while (true)
            {
                if (System.IO.File.Exists(coloredNamesFile))
                {
                    int i = 0;
                    List<string> lines = File.ReadLines(coloredNamesFile).ToList();
                    lines = lines.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
                    List<int> linesToRemove = new List<int>();
                    foreach (string line in lines)
                    {
                        if (line == null)
                        {
                            return;
                        }

                        System.String[] substrings = line.Split(',');
                        string userId = substrings[0];
                        System.DateTime expireDate = System.Convert.ToDateTime(substrings[1]);
                        if (expireDate < System.DateTime.Now)
                        {
                            try
                            {
                                IGuild guild = NadekoBot.Client.GetGuilds().First();
                                IGuildUser user = NadekoBot.Client.GetGuilds().First().GetUser(System.Convert.ToUInt64(userId));
                                if (user != null)
                                {
                                    await user.SendMessageAsync("Whoops! Looks like your colored name has just **expired**! Sorry about that! You can renew your colored name by buying it again at any time in the shop. (e.g. $shop namecolor red, or $shop namecolor #ff0000)");
                                    await clearColors(guild, user);
                                }

                                linesToRemove.Add(i);
                            }
                            catch (System.Exception e)
                            {
                                System.Console.WriteLine("Something went wrong in CheckForExpiredColors method.\n\nError:\n" + e);
                            }
                        }
                        i++;
                    }
                    if (linesToRemove.Count > 0)
                    {
                        int x = 0;
                        foreach (int lineNumber in linesToRemove)
                        {
                            lines.RemoveAt(lineNumber - x);
                            x++;
                        }
                        File.WriteAllLines(coloredNamesFile, lines);
                    }
                }
                await Task.Delay(System.DateTime.Now.AddHours(12).Millisecond);
            }
        }

        public static long GetCurrency(ulong id)
        {
            using (var uow = DbHandler.UnitOfWork())
            {
                return uow.Currency.GetUserCurrency(id);
            }
        }

        bool ColorsAreClose(ImageSharp.Color a, ImageSharp.Color z, int threshold = 50)
        {
            int r = (int)a.R - z.R,
                g = (int)a.G - z.G,
                b = (int)a.B - z.B;
            return (r * r + g * g + b * b) <= threshold * threshold;
        }

        [NadekoCommand, Usage, Description, Aliases]
        [RequireContext(ContextType.Guild)]
        public async Task Raffle([Remainder] IRole role = null)
        {
            role = role ?? Context.Guild.EveryoneRole;

            var members = role.Members().Where(u => u.Status != UserStatus.Offline && u.Status != UserStatus.Unknown);
{'fraction_non_alphanumeric': 0.09091844081044945, 'fraction_numerical': 0.003908258767869999, 'mean_word_length': 1.2703712351155731, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1679049', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2362, 'n_words': 537}