Esempio n. 1
0
        /// <summary>
        /// The display async.
        /// </summary>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        public async Task DisplayAsync()
        {
            var message = await Context.Channel.SendMessageAsync(_data.Text, embed : _data.Embed).ConfigureAwait(false);

            Message = message;
            _interactive.AddReactionCallback(message, this);

            _ = Task.Run(async() =>
            {
                foreach (var item in _data.Callbacks)
                {
                    if (item.AddReaction == false)
                    {
                        continue;
                    }
                    await message.AddReactionAsync(item.Reaction);
                }
            });

            if (Timeout.HasValue)
            {
                _ = Task.Delay(Timeout.Value)
                    .ContinueWith(_ =>
                {
                    _interactive.RemoveReactionCallback(message);
                    _data.TimeoutCallback?.Invoke(Context);
                });
            }
        }
Esempio n. 2
0
        public async Task DisplayAsync()
        {
            var embed   = BuildEmbed();
            var message = await Context.Client.CreateMessageAsync(Context.Channel.Id, new CreateMessageParams
            {
                Embed = embed
            });

            Message = message;
            _interactive.AddReactionCallback(message, this);
            _ = Task.Run(async() =>
            {
                await Context.Client.CreateReactionAsync(Context.Channel.Id, message.Id, Options.First);
                await Context.Client.CreateReactionAsync(Context.Channel.Id, message.Id, Options.Back);
                await Context.Client.CreateReactionAsync(Context.Channel.Id, message.Id, Options.Next);
                await Context.Client.CreateReactionAsync(Context.Channel.Id, message.Id, Options.Last);
                await Context.Client.CreateReactionAsync(Context.Channel.Id, message.Id, Options.Stop);
                await Context.Client.CreateReactionAsync(Context.Channel.Id, message.Id, Options.Info);
            });
            if (Timeout.HasValue)
            {
                _ = Task.Delay(Timeout.Value).ContinueWith(_ =>
                {
                    _interactive.RemoveReactionCallback(message);
                    _ = Context.Client.DeleteMessageAsync(Context.Channel.Id, Message.Id);
                });
            }
        }
Esempio n. 3
0
        public static async Task <IUserMessage> SendInteractiveMessageAsync
        (
            this InteractiveService @this,
            [NotNull] SocketCommandContext context,
            [NotNull] IInteractiveMessage interactiveMessage,
            [CanBeNull] ISocketMessageChannel channel = null
        )
        {
            channel = channel ?? context.Channel;

            var message = await interactiveMessage.DisplayAsync(channel);

            if (interactiveMessage.ReactionCallback is null)
            {
                return(message);
            }

            @this.AddReactionCallback(message, interactiveMessage.ReactionCallback);

            if (interactiveMessage.Timeout.HasValue)
            {
                // ReSharper disable once AssignmentIsFullyDiscarded
                _ = Task.Delay(interactiveMessage.Timeout.Value).ContinueWith(c =>
                {
                    @this.RemoveReactionCallback(interactiveMessage.Message);
                    interactiveMessage.Message.DeleteAsync();
                });
            }

            return(message);
        }
Esempio n. 4
0
        private async Task GetNextQuestionAsync()
        {
            if (status == TriviaStatus.Stopped)
            {
                return;
            }
            if (currentQuestionMessage != null)
            {
                interactiveService.RemoveReactionCallback(currentQuestionMessage);
                int points = QuestionToPoints(currentQuestion);

                var embedBuilder = new EmbedBuilder()
                                   .WithColor(new Color(46, 191, 84))
                                   .WithTitle("Time is up")
                                   .WithDescription($"The correct answer was: **{ currentQuestion.CorrectAnswer}**");

                string msg = "";
                if (correctAnswerUsers.Count > 0)
                {
                    correctAnswerUsers.ForEach(async usr => await AddPointsToUserAsync(usr, points).ConfigureAwait(false));
                    msg = $"*{string.Join(", ", correctAnswerUsers.Select(u => u.Username))}* had it right! Here, have {points} point{(points == 1 ? "" : "s")}.";
                }
                else
                {
                    msg = "No one had it right";
                }
                embedBuilder.AddField("Correct answers", msg, true);
                if (wrongAnswerUsers.Count > 0)
                {
                    wrongAnswerUsers.ForEach(async usr => await AddPointsToUserAsync(usr, -1).ConfigureAwait(false));
                    msg = $"*{string.Join(", ", wrongAnswerUsers.Select(u => u.Username))}* had it wrong! You lose 1 point.";
                }
                else
                {
                    msg = "No one had it wrong.";
                }
                embedBuilder.AddField("Incorrect answers", msg, true);

                var highScores = GetCurrentHighScores(3);
                if (!highScores.IsEmptyOrWhiteSpace())
                {
                    embedBuilder.AddField("Top 3:", highScores, true);
                }

                await MonkeyHelpers.SendChannelMessageAsync(discordClient, guildID, channelID, "", embed : embedBuilder.Build()).ConfigureAwait(false);

                correctAnswerUsers.Clear();
                wrongAnswerUsers.Clear();
            }
            if (currentIndex < questionsToPlay)
            {
                if (currentIndex >= questions.Count)                    // we want to play more questions than available
                {
                    await LoadQuestionsAsync(10).ConfigureAwait(false); // load more questions
                }
                currentQuestion = questions.ElementAt(currentIndex);
                var builder = new EmbedBuilder
                {
                    Color = new Color(26, 137, 185),
                    Title = $"Question {currentIndex + 1}"
                };
                int points = QuestionToPoints(currentQuestion);
                builder.Description = $"{currentQuestion.Category} - {currentQuestion.Difficulty} : {points} point{(points == 1 ? "" : "s")}";
                if (currentQuestion.Type == TriviaQuestionType.TrueFalse)
                {
                    builder.AddField($"{currentQuestion.Question}", "True or false?");
                    var trueEmoji          = new Emoji("👍");
                    var falseEmoji         = new Emoji("👎");
                    var correctAnswerEmoji = currentQuestion.CorrectAnswer.Equals("true", StringComparison.OrdinalIgnoreCase) ? trueEmoji : falseEmoji;

                    currentQuestionMessage = await interactiveService.SendMessageWithReactionCallbacksAsync(commandContext,
                                                                                                            new ReactionCallbackData("", builder.Build(), false, true, true, timeout, _ => GetNextQuestionAsync())
                                                                                                            .WithCallback(trueEmoji, (c, r) => CheckAnswer(r, correctAnswerEmoji))
                                                                                                            .WithCallback(falseEmoji, (c, r) => CheckAnswer(r, correctAnswerEmoji)),
                                                                                                            false
                                                                                                            ).ConfigureAwait(false);
                }
                else if (currentQuestion.Type == TriviaQuestionType.MultipleChoice)
                {
                    // add the correct answer to the list of correct answers to form the list of possible answers
                    var    answers = currentQuestion.IncorrectAnswers.Append(currentQuestion.CorrectAnswer);
                    Random rand    = new Random();
                    // randomize the order of the answers
                    var randomizedAnswers  = answers.OrderBy(_ => rand.Next()).ToList();
                    var correctAnswerEmoji = new Emoji(MonkeyHelpers.GetUnicodeRegionalLetter(randomizedAnswers.IndexOf(currentQuestion.CorrectAnswer)));
                    builder.AddField($"{currentQuestion.Question}", string.Join(Environment.NewLine, randomizedAnswers.Select((s, i) => $"{MonkeyHelpers.GetUnicodeRegionalLetter(i)} {s}")));

                    currentQuestionMessage = await interactiveService.SendMessageWithReactionCallbacksAsync(commandContext,
                                                                                                            new ReactionCallbackData("", builder.Build(), false, true, true, timeout, _ => GetNextQuestionAsync())
                                                                                                            .WithCallback(new Emoji(MonkeyHelpers.GetUnicodeRegionalLetter(0)), (c, r) => CheckAnswer(r, correctAnswerEmoji))
                                                                                                            .WithCallback(new Emoji(MonkeyHelpers.GetUnicodeRegionalLetter(1)), (c, r) => CheckAnswer(r, correctAnswerEmoji))
                                                                                                            .WithCallback(new Emoji(MonkeyHelpers.GetUnicodeRegionalLetter(2)), (c, r) => CheckAnswer(r, correctAnswerEmoji))
                                                                                                            .WithCallback(new Emoji(MonkeyHelpers.GetUnicodeRegionalLetter(3)), (c, r) => CheckAnswer(r, correctAnswerEmoji))
                                                                                                            , false).ConfigureAwait(false);
                }
                currentIndex++;
            }
            else
            {
                await StopTriviaAsync().ConfigureAwait(false);
            }
        }