예제 #1
0
    public async static Task HandleUploadCommand(SocketMessage message, CommunityRepository repository)
    {
        var attachments = message.Attachments;

        if (attachments.Count == 0)
        {
            await message.Channel.SendMessageAsync("You did not attach a zip file to your message. Please try again.");

            return;
        }

        if (attachments.Count > 1)
        {
            await message.Channel.SendMessageAsync("Please upload only one stage package at a time.");

            return;
        }

        Attachment attachment  = attachments.ElementAt(0);
        string     oldFileName = attachment.Filename;
        bool       changesMade = false;

        if (oldFileName.Contains("-") || oldFileName.Contains("_"))
        {
            changesMade = true;
        }

        string fileName = oldFileName.Replace("-", " ").Replace("_", " ");

        Stage stage = new Stage()
        {
            Name   = Path.GetFileNameWithoutExtension(fileName),
            Author = String.Format("{0}#{1}", message.Author.Username.ToLower(), message.Author.Discriminator.ToLower()),
            Date   = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString()
        };

        if (repository.StageExists(stage))
        {
            string response = String.Format("This stage already exists in the community repository. If you are the author of the existing stage, please use !update instead to update your stage.");
            await message.Channel.SendMessageAsync(response);

            return;
        }

        string URL = attachment.Url;

        using (WebClient client = new WebClient())
        {
            client.DownloadFile(new Uri(URL), fileName);
        }

        DamnedPackage package = new DamnedPackage();

        await message.Channel.SendMessageAsync("Checking your stage archive now...");

        if (!package.Check(fileName))
        {
            string reason = package.reasonForFailedCheck;
            await message.Channel.SendMessageAsync(reason);

            Directory.Delete(package.tempDirectory, true);
            Directory.Delete(fileName);
            return;
        }

        await message.Channel.SendMessageAsync("Stage check successful! I am adding your stage into the community repository now...");

        repository.AddStage(stage);
        string commitMessage = String.Format("Added in new stage {0} to the community repository", stage.Name);

        MainClass.UpdateGitHub(commitMessage);
        Directory.Delete(package.tempDirectory, true);

        if (changesMade)
        {
            string response = String.Format("Success! Your stage has been added into the community repository!\n\nBy the way, I have renamed your zip file from \"{0}\" to \"{1}.zip\" because it looks nicer and is slightly easier to search for", fileName, stage.Name);
            await message.Channel.SendMessageAsync(response);

            return;
        }

        await message.Channel.SendMessageAsync("Success! Your stage has been added into the community repository!");
    }