<commit_before>﻿using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;

namespace CompatBot.Commands
{
    internal partial class Sudo
    {
        [Group("bot")]
        [Description("Commands to manage the bot instance")]
        public sealed class Bot: BaseCommandModule
        {
            [Command("version")]
            [Description("Returns currently checked out bot commit")]
            public async Task Version(CommandContext ctx)
            {
                var typingTask = ctx.TriggerTypingAsync();
                using (var git = new Process
                {
                    StartInfo = new ProcessStartInfo("git", "log -1 --oneline")
                    {
                        CreateNoWindow = true,
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        StandardOutputEncoding = Encoding.UTF8,
                    },
                })
                {
                    git.Start();
                    var stdout = await git.StandardOutput.ReadToEndAsync().ConfigureAwait(false);
                    git.WaitForExit();
                    if (!string.IsNullOrEmpty(stdout))
                        await ctx.RespondAsync("```" + stdout + "```").ConfigureAwait(false);
                }
                await typingTask.ConfigureAwait(false);
            }

            [Command("restart"), Aliases("update")]
            [Description("Restarts bot and pulls newest commit")]
            public async Task Restart(CommandContext ctx)
            {
                var typingTask = ctx.TriggerTypingAsync();
                if (Monitor.TryEnter(updateObj))
                {
                    try
                    {
                        using (var git = new Process
                        {
                            StartInfo = new ProcessStartInfo("git", "pull")
                            {
                                CreateNoWindow = true,
                                UseShellExecute = false,
                                RedirectStandardOutput = true,
                                StandardOutputEncoding = Encoding.UTF8,
                            },
                        })
                        {
                            git.Start();
                            var stdout = await git.StandardOutput.ReadToEndAsync().ConfigureAwait(false);
                            git.WaitForExit();
                            if (!string.IsNullOrEmpty(stdout))
                                await ctx.RespondAsync("```" + stdout + "```").ConfigureAwait(false);
                        }
                        await ctx.RespondAsync("Restarting...").ConfigureAwait(false);
                        using (var self = new Process
                        {
#if DEBUG
                            StartInfo = new ProcessStartInfo("dotnet", $"run -- {Config.Token} {ctx.Channel.Id}")
#else
                            StartInfo = new ProcessStartInfo("dotnet", $"run -c Release -- {Config.Token} {ctx.Channel.Id}")
#endif
                        })
                        {
                            self.Start();
                            Config.Cts.Cancel();
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        await ctx.RespondAsync("Updating failed: " + e.Message).ConfigureAwait(false);
                    }
                    finally
                    {
                        Monitor.Exit(updateObj);
                    }
                }
                else
                    await ctx.RespondAsync("Update is already in progress").ConfigureAwait(false);
                await typingTask.ConfigureAwait(false);
            }

            [Command("stop"), Aliases("exit", "shutdown", "terminate")]
            [Description("Stops the bot. Useful if you can't find where you left one running")]
            public async Task Stop(CommandContext ctx)
            {
                await ctx.RespondAsync(ctx.Channel.IsPrivate<commit_msg>fix autoupdating in case it failed<commit_after>﻿using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CompatBot.Utils;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;

namespace CompatBot.Commands
{
    internal partial class Sudo
    {
        [Group("bot")]
        [Description("Commands to manage the bot instance")]
        public sealed class Bot: BaseCommandModule
        {
            [Command("version")]
            [Description("Returns currently checked out bot commit")]
            public async Task Version(CommandContext ctx)
            {
                var typingTask = ctx.TriggerTypingAsync();
                using (var git = new Process
                {
                    StartInfo = new ProcessStartInfo("git", "log -1 --oneline")
                    {
                        CreateNoWindow = true,
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        StandardOutputEncoding = Encoding.UTF8,
                    },
                })
                {
                    git.Start();
                    var stdout = await git.StandardOutput.ReadToEndAsync().ConfigureAwait(false);
                    git.WaitForExit();
                    if (!string.IsNullOrEmpty(stdout))
                        await ctx.RespondAsync("```" + stdout + "```").ConfigureAwait(false);
                }
                await typingTask.ConfigureAwait(false);
            }

            [Command("restart"), Aliases("update")]
            [Description("Restarts bot and pulls newest commit")]
            public async Task Restart(CommandContext ctx)
            {
                var typingTask = ctx.TriggerTypingAsync();
                if (Monitor.TryEnter(updateObj))
                {
                    try
                    {
                        using (var git = new Process
                        {
                            StartInfo = new ProcessStartInfo("git", "pull")
                            {
                                CreateNoWindow = true,
                                UseShellExecute = false,
                                RedirectStandardOutput = true,
                                StandardOutputEncoding = Encoding.UTF8,
                            },
                        })
                        {
                            git.Start();
                            var stdout = await git.StandardOutput.ReadToEndAsync().ConfigureAwait(true);
                            git.WaitForExit();
                            if (!string.IsNullOrEmpty(stdout))
                                await ctx.SendAutosplitMessageAsync("```" + stdout + "```").ConfigureAwait(true);
                        }
                        await ctx.RespondAsync("Restarting...").ConfigureAwait(true);
                        using (var self = new Process
                        {
#if DEBUG
                            StartInfo = new ProcessStartInfo("dotnet", $"run -- {Config.Token} {ctx.Channel.Id}")
#else
                            StartInfo = new ProcessStartInfo("dotnet", $"run -c Release -- {Config.Token} {ctx.Channel.Id}")
#endif
                        })
                        {
                            self.Start();
                            Config.Cts.Cancel();
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        await ctx.RespondAsync("Updating failed: " + e.Message).ConfigureAwait(true);
                    }
                    finally
                    {
                        Monitor.Exit(updateObj);
                    }
                }
                else
                    await ctx.RespondAsync("Update is already in progress").ConfigureAwait(false);
                await typingTask.ConfigureAwait(false);
            }

            [Command("stop"), Aliases("exit", "shutdown", "terminate")]
            [Description("Stops the bot. Useful if you can't find where you left one running")]
            public async Task Stop(CommandContext ctx)
            {
                await ctx.RespondAsync(ctx.Channel.IsPrivate
{'fraction_non_alphanumeric': 0.08755706426313942, 'fraction_numerical': 0.0007023293924850755, 'mean_word_length': 1.1070283600493218, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2988170', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1813, 'n_words': 402}