Beispiel #1
0
        public async Task ReplayConverterWithAddons(CommandContext ctx, params string[] addons)
        {
            try
            {
                await ctx.TriggerTypingAsync();

                JobInfo addonsJobInfo = addons != null?JobInfo.CreateFromStrings((byte)addons.Length, addons) : JobInfo.NoAddons;

                currentChannel = ctx.Channel;


                if (ctx.Message.Attachments.Count != 0)
                {
                    if (ctx.Message.Attachments.First().FileSize > 300000)
                    {
                        await ctx.RespondAsync("File is too big.");

                        return;
                    }

                    using (WebClient wwwClient = new WebClient())
                    {
                        wwwClient.DownloadFile(ctx.Message.Attachments.First().Url, ctx.Message.Attachments.First().FileName);
                    }

                    try
                    {
                        FileInfo replay    = new FileInfo(ctx.Message.Attachments.First().FileName);
                        string   replayID  = Path.GetRandomFileName() + ".lmp";
                        byte[]   fileBytes = File.ReadAllBytes(replay.FullName).ToArray();
                        string   addonPath;
                        string   version;
                        if (fileBytes[12] == 201)
                        {
                            addonPath = $"/root/.srb2/.srb21/addons/";
                            version   = "2.1";
                        }
                        else if (fileBytes[12] == 202)
                        {
                            addonPath = $"/root/.srb2/addons";
                            version   = "2.2";
                        }
                        else
                        {
                            await ctx.RespondAsync("File not playable on 2.2 or 2.1. Is it a valid replay?");

                            return;
                        }
                        File.Move(replay.Name, $"/root/.srb2/replaystogif/{replayID}");

                        string confirmationMessage = $"Processing {version} replay sent by {ctx.Member.Username}";
                        if (addons != null)
                        {
                            for (int i = 0; i < addons.Length; i++)
                            {
                                if (version == "2.1")
                                {
                                    if (!File.Exists($"{addonPath}/{addons[i]}"))
                                    {
                                        await ctx.RespondAsync("Addon does not exist on the server.");

                                        File.Delete($"/root/.srb2/replaystogif/{replayID}");
                                        return;
                                    }
                                }
                                else
                                {
                                    if (!File.Exists($"{addonPath}/Levels/{addons[i]}") && !File.Exists($"{addonPath}/Characters/{addons[i]}"))
                                    {
                                        await ctx.RespondAsync("Addon does not exist on the server.");

                                        File.Delete($"/root/.srb2/replaystogif/{replayID}");
                                        return;
                                    }
                                }
                            }
                            confirmationMessage += " with addon(s) " + string.Join(" ", addons);
                        }

                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))

                        {
                            await ctx.RespondAsync(confirmationMessage);

                            Program.convertQueue.Add(ctx);
                        }

                        Program.replayEvents.AddToQueue(addonsJobInfo, $"/root/.srb2/replaystogif/{replayID}", "/var/www/html/gifs/torename.gif");
                    }
                    catch (Exception e)
                    {
                        await ctx.RespondAsync("Error: " + e.Message);

                        // await ctx.RespondAsync(e.StackTrace);
                        // await ctx.RespondAsync(e.Source);
                        // await ctx.RespondAsync(e.InnerException.Message);
                    }
                }
                else
                {
                    await ctx.RespondAsync("No file attached.");
                }
            }
            catch (Exception e)
            {
                await ctx.RespondAsync("Error: " + e.Message);

                // await ctx.RespondAsync(e.StackTrace);
                // await ctx.RespondAsync(e.Source);
                // await ctx.RespondAsync(e.InnerException.Message);
            }
        }