Esempio n. 1
0
        public async Task Get(long gamerScore, AchievementType type, [Remainder] string achievementName)
        {
            Bot.Log(new LogMessage(LogSeverity.Debug, "AchievementGeneratorCommands", $"Generating image for user {Context.User.Id} in {Context.Guild.Id}"));

            // for 'work in progress' patterns, set them to use the one that works
            bool workInProgress = type == AchievementType.Xbox360;

            if (workInProgress)
            {
                type = AchievementType.XboxOne;
            }

            // if gamerScore unset, pick something random
            if (gamerScore == -1)
            {
                // use a rare score
                if (type == AchievementType.XboxOneRare)
                {
                    gamerScore = gamerScoreGenerator.GetRareGamerScore;
                }
                else
                {
                    gamerScore = gamerScoreGenerator.GetGamerScore;
                }
            }

            // tell the user that I'm working on it
            IUserMessage ack = await ReplyAsync($"Right away, {Context.User.Mention}!").ConfigureAwait(true);

            await Task.Factory.StartNew(action : () =>
            {
                // actually go and generate this
                imageGenerator.GenerateImage(achievementName, gamerScore, type, Context.Message.Id);
            },
                                        cancellationToken : CancellationToken.None,
                                        creationOptions : TaskCreationOptions.None,
                                        scheduler : TaskScheduler.Default)
            .ConfigureAwait(false);

            // remove the ack once file has been generated
            // gotta be a better way for this
            await ack.DeleteAsync().ConfigureAwait(true);

            // tell the user if the pattern they chose is WIP
            string prefix = "";

            if (workInProgress)
            {
                prefix = "That one is a work in progress.\n";
            }

            // reply with the image and some text
            await Context.Channel.SendFileAsync(
                imageGenerator.GenerateImagePath(Context.Message.Id),
                $"{prefix}Generated by {Context.User.Mention}.").ConfigureAwait(true);

            // wait a second
            await Task.Delay(1000).ConfigureAwait(true);

            // delete that old image from disk
            try
            {
                imageGenerator.DeleteImage(Context.Message.Id);
            }
            catch (Exception e)
            {
                // log errors when trying to delete the acknowledgement message
                Bot.Log(new LogMessage(LogSeverity.Warning, "AchievementGeneratorCommands", "Couldn't delete the message acknowledge message.", e));
            }
        }