<commit_before>﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Humanizer;
using Humanizer.Localisation;
using SoraBot_v2.Data;
using SoraBot_v2.Services;

namespace SoraBot_v2.Module
{
    public class SelfAssignableRolesModule : ModuleBase<SocketCommandContext>
    {
        private readonly SelfAssignableRolesService _sarService;

        public SelfAssignableRolesModule(SelfAssignableRolesService service)
        {
            _sarService = service;
        }

        [Command("addsar"), Alias("asar", "addrole"),
         Summary("Adds a self assignable role to the list. If it doesn't exist sora will create it")]
        public async Task AddSar(string roleName, int cost = 0, [Remainder] string expires = null)
        {
            if (cost == 0)
            {
                Console.WriteLine("No additional info");
                await _sarService.AddSarToList(Context, roleName.Trim());
                return;
            }

            if (string.IsNullOrWhiteSpace(expires))
            {
                Console.WriteLine($"Cost but no time. COST: {cost}");
                await _sarService.AddSarToList(Context, roleName.Trim(), false, cost);
                return;
            }
            
            // parse time
            var time = GetTime(expires);
            // check if time was valid
            if (time == null)
            {
                await ReplyAsync("",
                    embed: Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2],
                        "Failed to parse time for expiration"));
                return;
            }
            TimeSpan timeSpan = new TimeSpan(time[0], time[1], time[2], time[3]);
            
            if (timeSpan.TotalMinutes < 10)
            {
                await ReplyAsync("",
                    embed: Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2],
                        "The role must be valid for at least 10 minutes!"));
                return;
            }
            
            Console.WriteLine($"Cost: {cost}, time: {time.ToString()}, timespan: {timeSpan.Humanize(2, maxUnit: TimeUnit.Day, minUnit: TimeUnit.Second, countEmptyUnits:true)}");
            
            await _sarService.AddSarToList(Context, roleName.Trim(), true, cost, timeSpan);
        }

        [Command("defaultrole"), Alias("drole", "default"), Summary("Sets a default role for when users join")]
        public async Task AddDefaultRole([Remainder] string roleName)
        {
                await _sarService.AddDefaultRole(Context, roleName.Trim());
                return;
        }

        [Command("expiring"), Summary("Shows all your expiring sars")]
        public async Task ShowExpiringSars()
        {
            using (var soraContext = new SoraContext())
            {
                var exp = soraContext.ExpiringRoles.Where(x => x.UserForeignId == Context.User.Id && x.GuildForeignId == Context.Guild.Id).ToList();
                if (exp.Count == 0)
                {
                    await ReplyAsync("", embed: Utility.ResultFeedback(
                        Utility.RedFailiureEmbed,
                        Utility.SuccessLevelEmoji[2],
                        "You don't have any expiring roles in this Guild!"));
                    return;
                }
                var eb = new EmbedBuilder()
                {
                    Color = Utility.PurpleEmbed,
                    ThumbnailUrl = Context.Guild.IconUrl ?? Utility.StandardDiscordAvatar,
                    Author = new EmbedAuthorBuilder()
                    {
                        IconUrl = Context.User.GetAvatarUrl() ?? Utility.StandardDiscordAvatar,
                        Name = Utility.GiveUsernameDiscrimComb(Context.User)
                    },
                    Title = $"Your Expiring Roles in {Context.Guild.Name}",
                };
                foreach (var role in exp)
                {
                    var r = Context.Guild.GetRole(role.RoleForeignId);
                    if(r== null)
                        continue;
                    eb.AddField(x =>
                    {
                        x.IsInline = false;
                        x.Name = r.Name;
                        x.Value = $"Expires on: {role.ExpiresAt.ToString("hh:mm:ss dd/MM/yyyy UTC")}";
                    });
                }
                await ReplyAsync("", embed: eb);
            }
        }

        [Command("toggledefault"), Alias("toggledef"), Summary("Toggles if default role is on or off")]
        public async Task ToggleDefault()
        {
            await _sarService.ToggleDefaultRole(Context);
        }

        [Command("rmsar"), Alias("rsar", "rmrole", "delrole"), Summary("Removes a self-assignable role")]
        public async Task RemoveSar([Remainder] string roleName)
        {
            await _sarService.RemoveSarFromList(Context, roleName.Trim());
        }

        [Command("iam"), Alias("sar"), Summary("Assigns the role to you if it exists")]
        public async Task IAmSar([Summary("Role name"),Remainder] string roleName)
        {
            await _sarService.IAmSar(Context, roleName.Trim());
        }

        [Command("iamnot"), Summary("Removes specified self assignable role from you")]
        public async Task IAmNotSar([Summary("Role name"), Remainder] string roleName)
        {
            await _sarService.IAmNotSar(Context, roleName.Trim());
        }
<commit_msg>make iam and iamnot run async bcs its alot of processing involved.<commit_after>﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Humanizer;
using Humanizer.Localisation;
using SoraBot_v2.Data;
using SoraBot_v2.Services;

namespace SoraBot_v2.Module
{
    public class SelfAssignableRolesModule : ModuleBase<SocketCommandContext>
    {
        private readonly SelfAssignableRolesService _sarService;

        public SelfAssignableRolesModule(SelfAssignableRolesService service)
        {
            _sarService = service;
        }

        [Command("addsar"), Alias("asar", "addrole"),
         Summary("Adds a self assignable role to the list. If it doesn't exist sora will create it")]
        public async Task AddSar(string roleName, int cost = 0, [Remainder] string expires = null)
        {
            if (cost == 0)
            {
                Console.WriteLine("No additional info");
                await _sarService.AddSarToList(Context, roleName.Trim());
                return;
            }

            if (string.IsNullOrWhiteSpace(expires))
            {
                Console.WriteLine($"Cost but no time. COST: {cost}");
                await _sarService.AddSarToList(Context, roleName.Trim(), false, cost);
                return;
            }
            
            // parse time
            var time = GetTime(expires);
            // check if time was valid
            if (time == null)
            {
                await ReplyAsync("",
                    embed: Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2],
                        "Failed to parse time for expiration"));
                return;
            }
            TimeSpan timeSpan = new TimeSpan(time[0], time[1], time[2], time[3]);
            
            if (timeSpan.TotalMinutes < 10)
            {
                await ReplyAsync("",
                    embed: Utility.ResultFeedback(Utility.RedFailiureEmbed, Utility.SuccessLevelEmoji[2],
                        "The role must be valid for at least 10 minutes!"));
                return;
            }
            
            Console.WriteLine($"Cost: {cost}, time: {time.ToString()}, timespan: {timeSpan.Humanize(2, maxUnit: TimeUnit.Day, minUnit: TimeUnit.Second, countEmptyUnits:true)}");
            
            await _sarService.AddSarToList(Context, roleName.Trim(), true, cost, timeSpan);
        }

        [Command("defaultrole"), Alias("drole", "default"), Summary("Sets a default role for when users join")]
        public async Task AddDefaultRole([Remainder] string roleName)
        {
                await _sarService.AddDefaultRole(Context, roleName.Trim());
                return;
        }

        [Command("expiring"), Summary("Shows all your expiring sars")]
        public async Task ShowExpiringSars()
        {
            using (var soraContext = new SoraContext())
            {
                var exp = soraContext.ExpiringRoles.Where(x => x.UserForeignId == Context.User.Id && x.GuildForeignId == Context.Guild.Id).ToList();
                if (exp.Count == 0)
                {
                    await ReplyAsync("", embed: Utility.ResultFeedback(
                        Utility.RedFailiureEmbed,
                        Utility.SuccessLevelEmoji[2],
                        "You don't have any expiring roles in this Guild!"));
                    return;
                }
                var eb = new EmbedBuilder()
                {
                    Color = Utility.PurpleEmbed,
                    ThumbnailUrl = Context.Guild.IconUrl ?? Utility.StandardDiscordAvatar,
                    Author = new EmbedAuthorBuilder()
                    {
                        IconUrl = Context.User.GetAvatarUrl() ?? Utility.StandardDiscordAvatar,
                        Name = Utility.GiveUsernameDiscrimComb(Context.User)
                    },
                    Title = $"Your Expiring Roles in {Context.Guild.Name}",
                };
                foreach (var role in exp)
                {
                    var r = Context.Guild.GetRole(role.RoleForeignId);
                    if(r== null)
                        continue;
                    eb.AddField(x =>
                    {
                        x.IsInline = false;
                        x.Name = r.Name;
                        x.Value = $"Expires on: {role.ExpiresAt.ToString("hh:mm:ss dd/MM/yyyy UTC")}";
                    });
                }
                await ReplyAsync("", embed: eb);
            }
        }

        [Command("toggledefault"), Alias("toggledef"), Summary("Toggles if default role is on or off")]
        public async Task ToggleDefault()
        {
            await _sarService.ToggleDefaultRole(Context);
        }

        [Command("rmsar"), Alias("rsar", "rmrole", "delrole"), Summary("Removes a self-assignable role")]
        public async Task RemoveSar([Remainder] string roleName)
        {
            await _sarService.RemoveSarFromList(Context, roleName.Trim());
        }

        [Command("iam", RunMode = RunMode.Async), Alias("sar"), Summary("Assigns the role to you if it exists")]
        public async Task IAmSar([Summary("Role name"),Remainder] string roleName)
        {
            await _sarService.IAmSar(Context, roleName.Trim());
        }

        [Command("iamnot", RunMode = RunMode.Async), Summary("Removes specified self assignable role from you")]
        public async Task IAmNotSar([Summary("Role name"), Remainder] string roleName)
        {
            await _sarService.IAmNotSar(Context, roleName.Trim());
        }

{'fraction_non_alphanumeric': 0.10157012330346847, 'fraction_numerical': 0.003193471125698572, 'mean_word_length': 1.7055435565154788, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '1084256', 'n_tokens_mistral': 3079, 'n_tokens_neox': 2881, 'n_words': 697}