/// <summary> /// Stops the current trivia. Returns false if trivia is not running /// </summary> /// <returns>success</returns> public async Task <bool> StopTriviaAsync() { if (!(status == TriviaStatus.Running)) { return(false); } var embedBuilder = new EmbedBuilder() .WithColor(new Color(46, 191, 84)) .WithTitle("The quiz has ended"); var currentScores = GetCurrentHighScores(); if (!currentScores.IsEmptyOrWhiteSpace()) { embedBuilder.AddField("Final scores:", currentScores, true); } var globalScores = await GetGlobalHighScoresAsync(int.MaxValue, guildID).ConfigureAwait(false); if (!globalScores.IsEmptyOrWhiteSpace()) { embedBuilder.AddField("Global top scores:", globalScores); } await MonkeyHelpers.SendChannelMessageAsync(discordClient, guildID, channelID, "", embed : embedBuilder.Build()).ConfigureAwait(false); userScoresCurrent.Clear(); status = TriviaStatus.Stopped; return(true); }
/// <summary> /// Starts a new quiz with the specified amount of questions /// </summary> /// <param name="questionsToPlay">Amount of questions to be played (max 50)</param> /// <returns>success</returns> public async Task <bool> StartTriviaAsync(int questionsToPlay) { if (questionsToPlay < 1) { _ = await MonkeyHelpers.SendChannelMessageAsync(discordClient, guildID, channelID, "At least one question has to be played").ConfigureAwait(false); return(false); } if (status == TriviaStatus.Running) { _ = await MonkeyHelpers.SendChannelMessageAsync(discordClient, guildID, channelID, "There is already a quiz running").ConfigureAwait(false); return(false); } this.questionsToPlay = questionsToPlay; questions?.Clear(); var embed = new EmbedBuilder() .WithColor(new Color(26, 137, 185)) .WithTitle("Trivia") .WithDescription( $"Starting trivia with {questionsToPlay} question{ (questionsToPlay == 1 ? "" : "s")}." + Environment.NewLine + "- Answer each question by clicking on the corresponding Emoji" + Environment.NewLine + "- Each question has a value of 1-3 points" + Environment.NewLine + $"- You have {timeout.TotalSeconds} seconds for each question." + "- Only your first answer counts!" + Environment.NewLine + "- Each wrong answer will reduce your points by 1 until you are back to zero" + Environment.NewLine ) .Build(); _ = await MonkeyHelpers.SendChannelMessageAsync(discordClient, guildID, channelID, "", embed : embed).ConfigureAwait(false); await LoadQuestionsAsync(questionsToPlay).ConfigureAwait(false); if (questions == null || questions.Count == 0) { _ = await MonkeyHelpers.SendChannelMessageAsync(discordClient, guildID, channelID, "Questions could not be loaded").ConfigureAwait(false); return(false); } userScoresCurrent = new Dictionary <ulong, int>(); currentQuestionMessage = null; correctAnswerUsers.Clear(); wrongAnswerUsers.Clear(); currentIndex = 0; status = TriviaStatus.Running; await GetNextQuestionAsync().ConfigureAwait(false); return(true); }