Example #1
0
    public async static Task HandleGetCommand(SocketMessage message, CommunityRepository repository)
    {
        string originalMessage = message.Content;

        string[] splitMessage = originalMessage.Split(new char[] { ' ' }, 2);
        string   stageName    = splitMessage[1];

        Stage stage = new Stage()
        {
            Name = stageName
        };

        string filePath = repository.GetPathToStageInCommunityRepository(stage);

        if (filePath == String.Empty)
        {
            string reply = String.Format(@"Failed  to find the stage ""{0}"". because it does not exist in the community repository. If you are not sure what you are looking for, use !search instead.", stageName);
            await message.Channel.SendMessageAsync(reply);

            return;
        }

        await message.Channel.SendMessageAsync("Uploading the stage now...");

        string response = "Here is the requested stage that you asked for.";
        await message.Channel.SendFileAsync(filePath, response);
    }
Example #2
0
    public async static Task HandleUpdateCommand(SocketMessage message, CommunityRepository repository)
    {
        var attachments = message.Attachments;

        if (attachments.Count == 0)
        {
            await message.Channel.SendMessageAsync("You did not upload a file for me to update in the community repository");

            return;
        }

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

            return;
        }

        var    attachment = message.Attachments.ElementAt(0);
        string stageName  = Path.GetFileNameWithoutExtension(attachment.Filename).Replace("_", " ").Replace("-", " ");

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

        if (!repository.StageExists(stage))
        {
            await message.Channel.SendMessageAsync("Failed to update the stage because the stage does not exist in the community repository.");

            return;
        }

        Stage repositoryStage = repository.GetStage(stage.Name);

        if (stage.Author != repositoryStage.Author)
        {
            await message.Channel.SendMessageAsync("Failed to update the stage because you are not the author of the stage.");

            return;
        }

        string URL          = attachment.Url;
        string fileName     = attachment.Filename.Replace("_", " ").Replace("-", " ");
        int    randomNumber = new Random().Next();
        string damnedStageCheckerDirectoryName = String.Format("DamnedStageChecker_{0}", randomNumber);
        string tempArchive       = Path.Combine(Path.GetTempPath(), damnedStageCheckerDirectoryName, fileName);
        string parentTempArchive = Path.Combine(Path.GetTempPath(), damnedStageCheckerDirectoryName);

        if (Directory.Exists(parentTempArchive))
        {
            Directory.Delete(parentTempArchive, true);
        }

        Directory.CreateDirectory(parentTempArchive);

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


        DamnedPackage package = new DamnedPackage();

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

        if (!package.Check(tempArchive))
        {
            Directory.Delete(parentTempArchive, true);
            Directory.Delete(package.tempDirectory, true);
            await message.Channel.SendMessageAsync(package.reasonForFailedCheck);

            return;
        }

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

        string oldFilePath = repository.GetPathToStageInCommunityRepository(repositoryStage);

        File.Delete(oldFilePath);
        File.Copy(tempArchive, fileName);
        Directory.Delete(parentTempArchive, true);
        Directory.Delete(package.tempDirectory, true);
        repository.UpdateStage(repositoryStage, stage);

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

        MainClass.UpdateGitHub(commitMessage);
        await message.Channel.SendMessageAsync("Succcessfully updated your stage that is in the community repository!");
    }