<commit_before>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Globalization;
using System.Net.Http;
using Discord.API;

namespace KiteBotCore.Modules
{
    public class Admin : CleansingModuleBase
    {
        private readonly CommandService _handler;
        private readonly IDependencyMap _map;

        public Admin(IDependencyMap map)
        {
            _handler = map.Get<CommandService>();
            _map = map;
        }

        [Command("save")]
        [Summary("saves markov chain messages")]
        [RequireOwner]
        public async Task SaveCommand()
        {
            var message = await ReplyAsync("OK");
            var saveTask = KiteChat.MultiDeepMarkovChains.SaveAsync();
            await saveTask.ContinueWith(async (e) =>
            {
                if (e.IsCompleted) await message.ModifyAsync(x => x.Content += ", Saved.");
            });
        }

        [Command("saveexit")]
        [Alias("se")]
        [Summary("saves and exits")]
        [RequireOwner]
        public async Task SaveExitCommand()
        {
            var message = await ReplyAsync("OK");
            var saveTask = KiteChat.MultiDeepMarkovChains.SaveAsync();
            await saveTask.ContinueWith(async (e) =>
            {
                if (e.IsCompleted) await message.ModifyAsync(x => x.Content += ", Saved.");
            });            
            Environment.Exit(0);
        }

        [Command("update")]
        [Alias("up")]
        [Summary("Updates the livestream channel, and probably crashes if there is no chat")]
        [RequireOwner]
        public async Task UpdateCommand()
        {
            await KiteChat.StreamChecker.ForceUpdateChannel();
            await ReplyAsync("updated?");
        }

        [Command("delete")]
        [Alias("del")]
        [Summary("Deletes the last message the bot has written")]
        [RequireOwner]
        public async Task DeleteCommand()
        {
            if (KiteChat.BotMessages.Any()) await ((IUserMessage) KiteChat.BotMessages.Last()).DeleteAsync();
        }

        [Command("restart")]
        [Alias("re")]
        [Summary("restarts the video and livestream checkers")]
        [RequireOwner]
        public async Task RestartCommand()
        {
            KiteChat.StreamChecker?.Restart();
            KiteChat.GbVideoChecker?.Restart();
            await ReplyAsync("It might have done something, who knows.");
        }

        [Command("ignore")]
        [Summary("ignore a gb chat channelname")]
        [RequireOwner]
        public async Task IgnoreCommand([Remainder] string input)
        {
            KiteChat.StreamChecker.IgnoreChannel(input);
            await ReplyAsync("Added to ignore list.");
        }

        [Command("listchannels")]
        [Summary("Lists names of GB chats")]
        [RequireOwner]
        public async Task ListChannelCommand()
        {

            await ReplyAsync("Current livestreams channels are:" + Environment.NewLine + (await KiteChat.StreamChecker.ListChannels()));
        }

        [Command("say")]
        [Alias("echo")]
        [Summary("Echos the provided input")]
        [RequireOwner]
        public async Task SayCommand([Remainder] string input)
        {
            await ReplyAsync(input);
        }

        [Command("embed")]
        [Summary("Echos the provided input")]
        [RequireOwner]
        public async Task EmbedCommand([Remainder] string input)
        {
            var embed = new EmbedBuilder();
            embed.Title = "Test";
            embed.Color = new Color(255,0,0);
            embed.Description = input;
            embed.AddField(x =>
            {
                x.IsInline = false;
                x.Name = "Image";
                x.Value = "http://static.giantbomb.com/uploads/square_avatar/13/137243/1887704-lassie_01.jpg";
            });
            await ReplyAsync( input, false, embed);
        }

        [Command("setgame")]
        [Alias("playing")]
        [Summary("Sets a game in discord")]
        [RequireOwner]
        public async Task PlayingCommand([Remainder] string input)
        {
            var client = _map.Get<DiscordSocketClient>();
            await client.SetGame(input);
        }

        [Command("setusername")]
        [Alias("username")]
        [Summary("Sets a game in discord")]
        [RequireOwner]
        public async Task UsernameCommand([Remainder] string input)
        {
            var client = _map.Get<DiscordSocketClient>();
            await client.CurrentUser.ModifyAsync(x => x.Username = input);
        }

        [Command("setusername")]
        [Alias("username")]
        [Summary("Sets a game in discord")]
        [RequireOwner,RequireContext(ContextType.Guild)]
        public async Task NicknameCommand([Remainder] string input)
        {
            await (await Context.Guild.GetCurrentUserAsync()).ModifyAsync(x => x.Nickname = input);
        }

        [Command("setavatar")]
        [Alias("avatar")]
        [Summary("Sets a new avatar image for this bot")]
        [RequireOwner]
        public async Task AvatarCommand([Remainder] string input)
        {
            var avatarStream = await new HttpClient().GetStreamAsync(input);
            await Context.Client.CurrentUser.ModifyAsync(x => x.Avatar = new Image(avatarStream));
            await ReplyAsync("👌");
        }

        [Command("help")]
        [Summary("Lists available commands")]
        public async Task Help(string optional = null)
        {
            string output = "";
            if (optional != null)
            {<commit_msg>Avatar setter command<commit_after>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Globalization;
using System.Net.Http;
using Discord.API;
using System.IO;

namespace KiteBotCore.Modules
{
    public class Admin : CleansingModuleBase
    {
        private readonly CommandService _handler;
        private readonly IDependencyMap _map;

        public Admin(IDependencyMap map)
        {
            _handler = map.Get<CommandService>();
            _map = map;
        }

        [Command("save")]
        [Summary("saves markov chain messages")]
        [RequireOwner]
        public async Task SaveCommand()
        {
            var message = await ReplyAsync("OK");
            var saveTask = KiteChat.MultiDeepMarkovChains.SaveAsync();
            await saveTask.ContinueWith(async (e) =>
            {
                if (e.IsCompleted) await message.ModifyAsync(x => x.Content += ", Saved.");
            });
        }

        [Command("saveexit")]
        [Alias("se")]
        [Summary("saves and exits")]
        [RequireOwner]
        public async Task SaveExitCommand()
        {
            var message = await ReplyAsync("OK");
            var saveTask = KiteChat.MultiDeepMarkovChains.SaveAsync();
            await saveTask.ContinueWith(async (e) =>
            {
                if (e.IsCompleted) await message.ModifyAsync(x => x.Content += ", Saved.");
            });            
            Environment.Exit(0);
        }

        [Command("update")]
        [Alias("up")]
        [Summary("Updates the livestream channel, and probably crashes if there is no chat")]
        [RequireOwner]
        public async Task UpdateCommand()
        {
            await KiteChat.StreamChecker.ForceUpdateChannel();
            await ReplyAsync("updated?");
        }

        [Command("delete")]
        [Alias("del")]
        [Summary("Deletes the last message the bot has written")]
        [RequireOwner]
        public async Task DeleteCommand()
        {
            if (KiteChat.BotMessages.Any()) await ((IUserMessage) KiteChat.BotMessages.Last()).DeleteAsync();
        }

        [Command("restart")]
        [Alias("re")]
        [Summary("restarts the video and livestream checkers")]
        [RequireOwner]
        public async Task RestartCommand()
        {
            KiteChat.StreamChecker?.Restart();
            KiteChat.GbVideoChecker?.Restart();
            await ReplyAsync("It might have done something, who knows.");
        }

        [Command("ignore")]
        [Summary("ignore a gb chat channelname")]
        [RequireOwner]
        public async Task IgnoreCommand([Remainder] string input)
        {
            KiteChat.StreamChecker.IgnoreChannel(input);
            await ReplyAsync("Added to ignore list.");
        }

        [Command("listchannels")]
        [Summary("Lists names of GB chats")]
        [RequireOwner]
        public async Task ListChannelCommand()
        {

            await ReplyAsync("Current livestreams channels are:" + Environment.NewLine + (await KiteChat.StreamChecker.ListChannels()));
        }

        [Command("say")]
        [Alias("echo")]
        [Summary("Echos the provided input")]
        [RequireOwner]
        public async Task SayCommand([Remainder] string input)
        {
            await ReplyAsync(input);
        }

        [Command("embed")]
        [Summary("Echos the provided input")]
        [RequireOwner]
        public async Task EmbedCommand([Remainder] string input)
        {
            var embed = new EmbedBuilder();
            embed.Title = "Test";
            embed.Color = new Color(255,0,0);
            embed.Description = input;
            embed.AddField(x =>
            {
                x.IsInline = false;
                x.Name = "Image";
                x.Value = "http://static.giantbomb.com/uploads/square_avatar/13/137243/1887704-lassie_01.jpg";
            });
            await ReplyAsync( input, false, embed);
        }

        [Command("setgame")]
        [Alias("playing")]
        [Summary("Sets a game in discord")]
        [RequireOwner]
        public async Task PlayingCommand([Remainder] string input)
        {
            var client = _map.Get<DiscordSocketClient>();
            await client.SetGame(input);
        }

        [Command("setusername")]
        [Alias("username")]
        [Summary("Sets a game in discord")]
        [RequireOwner]
        public async Task UsernameCommand([Remainder] string input)
        {
            var client = _map.Get<DiscordSocketClient>();
            await client.CurrentUser.ModifyAsync(x => x.Username = input);
        }

        [Command("setusername")]
        [Alias("username")]
        [Summary("Sets a game in discord")]
        [RequireOwner,RequireContext(ContextType.Guild)]
        public async Task NicknameCommand([Remainder] string input)
        {
            await (await Context.Guild.GetCurrentUserAsync()).ModifyAsync(x => x.Nickname = input);
        }

        [Command("setavatar",RunMode = RunMode.Sync)]
        [Alias("avatar")]
        [Summary("Sets a new avatar image for this bot")]
        [RequireOwner]
        public async Task AvatarCommand([Remainder] string input)
        {
            var avatarStream = await new HttpClient().GetByteArrayAsync(input);
            Stream stream = new MemoryStream(avatarStream);
            await Context.Client.CurrentUser.ModifyAsync(x => x.Avatar = new Image(stream));
            await ReplyAsync("👌");
        }

        [Command("help")]
        [Summary("Lists available commands")]
        public async Task Help(string optional = null)
        {
            string output = "";
            if (optional != null)
            {
{'fraction_non_alphanumeric': 0.1142142918713904, 'fraction_numerical': 0.003965175415912421, 'mean_word_length': 2.0355834641548927, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, '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': '5503865', 'n_tokens_mistral': 3081, 'n_tokens_neox': 3014, 'n_words': 715}