Exemplo n.º 1
0
        /// <summary>
        ///     Allows moderator to update or delete a playtest request. If confirmed, the event is added to the calendar.
        /// </summary>
        /// <returns></returns>
        private async Task ConfirmSchedule()
        {
            while (true)
            {
                //Variables used later.
                var index   = -1;
                var isValid = false;

                await Display(
                    "Type the ID of the field you want to edit or type `Schedule` to schedule the playtest.\n" +
                    "Type `delete` to delete this request completely.\n" +
                    "Type `save` to save the request and exit.");

                //User's input
                _userMessage = await _interactive.NextMessageAsync(_context);

                //Exiting
                if (_userMessage == null ||
                    _userMessage.Content.Equals("exit", StringComparison.OrdinalIgnoreCase))
                {
                    await CancelRequest();

                    return;
                }

                //We want to schedule the event
                if (_userMessage.Content.Equals("schedule", StringComparison.OrdinalIgnoreCase))
                {
                    //Server cannot be none when scheduling. Require it to be valid.
                    if (_testRequest.Preferredserver == "No preference")
                    {
                        index   = 11;
                        isValid = true;
                    }
                    else
                    {
                        break;
                    }
                }

                if (_userMessage.Content.Equals("save", StringComparison.OrdinalIgnoreCase))
                {
                    var saveResult = DatabaseUtil.UpdatePlaytestRequests(_testRequest);
                    var saveEmbed  = RebuildEmbed();
                    if (saveResult)
                    {
                        saveEmbed.WithColor(55, 55, 165);
                        await _instructionsMessage.ModifyAsync(x => x.Content = "Request saved!");

                        await _embedMessage.ModifyAsync(x => x.Embed = saveEmbed.Build());
                    }
                    else
                    {
                        saveEmbed.WithColor(165, 55, 55);
                        await _instructionsMessage.ModifyAsync(x =>
                                                               x.Content = "There was an issue saving the request");

                        await _embedMessage.ModifyAsync(x => x.Embed = saveEmbed.Build());
                    }

                    return;
                }

                //Deleting test
                if (_userMessage.Content.Equals("delete", StringComparison.OrdinalIgnoreCase))
                {
                    DatabaseUtil.RemovePlaytestRequest(_testRequest);
                    await _embedMessage.ModifyAsync(x => x.Embed = RebuildEmbed().WithColor(25, 25, 25).Build());

                    await _instructionsMessage.ModifyAsync(x => x.Content = "Request Deleted!");

                    await _userMessage.DeleteAsync();

                    return;
                }

                //Get the relevant index from the user message only if we aren't getting here from a forced server schedule.
                if (!isValid)
                {
                    isValid = int.TryParse(_userMessage.Content, out index);
                }

                //Valid number, and between the valid indexes?
                if (isValid && index >= 0 && index <= _arrayValues.Length)
                {
                    //Validate based on the index.
                    await Display(_wizardText[index]);

                    _userMessage = await _interactive.NextMessageAsync(_context);

                    if (_userMessage == null ||
                        _userMessage.Content.Equals("exit", StringComparison.OrdinalIgnoreCase))
                    {
                        await CancelRequest();

                        return;
                    }

                    //Loop until valid, or allow an exit.
                    while (!await ParseTestInformation(_arrayValues[index], _userMessage.Content))
                    {
                        //Invalid, let's try again.
                        _userMessage = await _interactive.NextMessageAsync(_context);

                        if (_userMessage.Content.Equals("exit", StringComparison.OrdinalIgnoreCase))
                        {
                            break;
                        }

                        //No reply - fully exit
                        if (_userMessage == null)
                        {
                            await CancelRequest();

                            return;
                        }
                    }
                }
            }

            //Scheduling
            if (!_context.IsPrivate)
            {
                await _userMessage.DeleteAsync();
            }

            var finalEmbed = RebuildEmbed();

            var eventAdded = await _calendar.AddTestEvent(_testRequest, _context.User);

            //Try added to calendar, if true we can move forward with alerting user of the schedule.
            if (eventAdded)
            {
                //Remove the test from the DB.
                DatabaseUtil.RemovePlaytestRequest(_testRequest);

                //Update display
                finalEmbed.WithColor(new Color(240, 240, 240));
                await _instructionsMessage.ModifyAsync(x => x.Content = "Test Scheduled!");

                await _embedMessage.ModifyAsync(x => x.Embed = finalEmbed.Build());

                //Build the string to mention all creators on the event.
                var    mentionChannel = _dataService.AdminBotsChannel;
                string mentions       = null;
                _testRequest.CreatorsDiscord.ForEach(x => mentions += $"{_dataService.GetSocketGuildUser(x).Mention} ");

                if (_testRequest.Game.Equals("csgo", StringComparison.OrdinalIgnoreCase))
                {
                    mentionChannel = _dataService.CSGOTestingChannel;
                }
                else if (_testRequest.Game.Equals("tf2", StringComparison.OrdinalIgnoreCase))
                {
                    mentionChannel = _dataService.TF2TestingChannel;
                }

                await mentionChannel.SendMessageAsync(
                    $"{mentions.Trim()} your playtest has been scheduled for `{_testRequest.TestDate}` (CT Timezone)");

                await _log.LogMessage($"{_context.User} has scheduled a playtest!\n{_testRequest}", color : LOG_COLOR);

                //Workshop embed.
                var ws      = new Workshop(_dataService, _log);
                var wbEmbed = await ws.HandleWorkshopEmbeds(_context.Message,
                                                            $"[Map Images]({_testRequest.ImgurAlbum}) | [Playtesting Information](https://www.tophattwaffle.com/playtesting)",
                                                            _testRequest.TestType, GeneralUtil.GetWorkshopIdFromFqdn(_testRequest.WorkshopURL));

                if (wbEmbed != null)
                {
                    await mentionChannel.SendMessageAsync(embed : wbEmbed.Build());
                }

                return;
            }

            //Failed to add to calendar.
            finalEmbed.WithColor(new Color(20, 20, 20));
            await _embedMessage.ModifyAsync(x => x.Embed = finalEmbed.Build());

            await _instructionsMessage.ModifyAsync(x => x.Content =
                                                   "An error occured working with the Google APIs, consult the logs.\n" +
                                                   "The playtest event may still have been created.");
        }