<commit_before>﻿using Discord;
using Discord.Commands;
using Discord.Modules;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DiscordBot.Modules.StarlightStage
{
    internal class StarlightStageModule : IModule
    {
        private ModuleManager _manager;
        private DiscordClient _client;
        private string filePath = "UmiBot.json";
        private List<Account> accounts;
        private string result, name, id;

        void IModule.Install(ModuleManager manager)
        {
            _manager = manager;
            _client = _manager.Client;

            LoadJson();

            manager.CreateCommands("ss", group =>
            {
                group.CreateCommand("get")
                       .Parameter("Text", ParameterType.Required)
                       .Description("Gets user info based off account ID")
                       .Do(async e =>
                       {
                           await e.Channel.SendIsTyping();
                           GetMe(e);
                       });
                group.CreateCommand("add")
                       .Parameter("Text", ParameterType.Unparsed)
                       .Description("Adds user info to an account list")
                       .Do(async e =>
                       {
                           await e.Channel.SendIsTyping();
                           AddMe(e);
                           //await e.Channel.SendMessage($"coming soon (๑◕︵◕๑)");
                       });
            });
        }

        private void LoadJson()
        {
            try
            {
                using (StreamReader r = new StreamReader(filePath))
                {
                    string json = r.ReadToEnd();
                    accounts = JsonConvert.DeserializeObject<List<Account>>(json);
                }
            }
            catch (Exception ex)
            {
                File.Create(filePath);
            }
        }

        private async void AddMe(CommandEventArgs e)
        {
            try
            {
                string[] parsedArgs = e.Args[0].Split(' ');
                name = parsedArgs[0];

                if (Regex.IsMatch(parsedArgs[1], "^[0-9]{9}$"))
                {
                    id = parsedArgs[1];
                }
                else
                {
                    throw new SystemException("Invalid ID");
                }

                Write(name, id);

                result = $"{name.ToString()} added";
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            await e.Channel.SendMessage($"{result}");
        }

        private async void GetMe(CommandEventArgs e)
        {
            try
            {

                id = Read(e.Args[0]);

                if (String.IsNullOrWhiteSpace(id))
                {
                    throw new SystemException();
                }

                result = $"https://deresute.me/{id}/medium";
            }
            catch (Exception ex)
            {
                if (Regex.IsMatch(e.Args[0], "^[0-9]{9}$"))
                {
                    result = $"https://deresute.me/{e.Args[0]}/medium";
                }
                else
                {
                    result = "Try again (๑◕︵◕๑)";
                }
            }

            await e.Channel.SendMessage($"{result}");
        }

        private void Write(string name, string id)
        {
            Account psyduck = new Account()
            {
                name = name,
                id = id
            };

            int index = accounts.FindLastIndex(s => s.name == psyduck.name);
            if (index != -1)
            {
                accounts[index] = psyduck;
            }
            else
            {
                accounts.Add(psyduck);
            }

            string json = JsonConvert.SerializeObject(accounts.ToArray());

            //write string to file
            System.IO.File.WriteAllText($"{filePath}", json);
        }

        private string Read(string name)
        {
            var result = "";

            foreach (Account a in accounts)
            {
                if (String.Equals(a.name, name, StringComparison.OrdinalIgnoreCase))
                {
                    result = a.id;
                }
            }

            return result;
        }
    }

    internal class Account
    {
        public string name;
        public string id;
    }
}<commit_msg>Initialize list; do not delete existing json file<commit_after>﻿using Discord;
using Discord.Commands;
using Discord.Modules;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DiscordBot.Modules.StarlightStage
{
    internal class StarlightStageModule : IModule
    {
        private ModuleManager _manager;
        private DiscordClient _client;
        private string filePath = "UmiBot.json";
        private List<Account> accounts = new List<Account>();
        private string result, name, id;

        void IModule.Install(ModuleManager manager)
        {
            _manager = manager;
            _client = _manager.Client;

            LoadJson();

            manager.CreateCommands("ss", group =>
            {
                group.CreateCommand("get")
                       .Parameter("Text", ParameterType.Required)
                       .Description("Gets user info based off account ID")
                       .Do(async e =>
                       {
                           await e.Channel.SendIsTyping();
                           GetMe(e);
                       });
                group.CreateCommand("add")
                       .Parameter("Text", ParameterType.Unparsed)
                       .Description("Adds user info to an account list")
                       .Do(async e =>
                       {
                           await e.Channel.SendIsTyping();
                           AddMe(e);
                       });
            });
        }

        private void LoadJson()
        {
            try
            {
                using (StreamReader r = new StreamReader(filePath))
                {
                    string json = r.ReadToEnd();
                    accounts = JsonConvert.DeserializeObject<List<Account>>(json);
                }
            }
            catch (Exception ex)
            {
                if (!File.Exists(filePath))
                    File.Create(filePath);
            }
        }

        private async void AddMe(CommandEventArgs e)
        {
            try
            {
                string[] parsedArgs = e.Args[0].Split(' ');
                name = parsedArgs[0];

                if (Regex.IsMatch(parsedArgs[1], "^[0-9]{9}$"))
                {
                    id = parsedArgs[1];
                }
                else
                {
                    throw new SystemException("Invalid ID");
                }

                Write(name, id);

                result = $"{name.ToString()} added";
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            await e.Channel.SendMessage($"{result}");
        }

        private async void GetMe(CommandEventArgs e)
        {
            try
            {
                id = Read(e.Args[0]);

                if (String.IsNullOrWhiteSpace(id))
                {
                    throw new SystemException();
                }

                result = $"https://deresute.me/{id}/medium";
            }
            catch (Exception ex)
            {
                if (Regex.IsMatch(e.Args[0], "^[0-9]{9}$"))
                {
                    result = $"https://deresute.me/{e.Args[0]}/medium";
                }
                else
                {
                    result = "Try again (๑◕︵◕๑)";
                }
            }

            await e.Channel.SendMessage($"{result}");
        }

        private void Write(string name, string id)
        {
            Account psyduck = new Account()
            {
                name = name,
                id = id
            };

            int index = accounts.FindLastIndex(s => s.name == psyduck.name);
            if (index != -1)
            {
                accounts[index] = psyduck;
            }
            else
            {
                accounts.Add(psyduck);
            }

            string json = JsonConvert.SerializeObject(accounts.ToArray());

            System.IO.File.WriteAllText($"{filePath}", json);
        }

        private string Read(string name)
        {
            foreach (Account a in accounts)
            {
                if (String.Equals(a.name, name, StringComparison.OrdinalIgnoreCase))
                {
                    return a.id;
                }
            }
            return "";
        }
    }

    internal class Account
    {
        public string name;
        public string id;
    }
}
{'fraction_non_alphanumeric': 0.09912347148576994, 'fraction_numerical': 0.0036792554918298886, 'mean_word_length': 1.0588104254845176, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 18, 'https://': 4, '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': '5562109', 'n_tokens_mistral': 2356, 'n_tokens_neox': 2223, 'n_words': 468}