public async Task <Villager> WereWolvesTurn(CommandContext ctx) { var baseTitle = "Who want you kill ?"; var embed = RandomCommands.CreateEmbed(baseTitle, false, _users.Keys.Select(_ => _.Username).ToArray()); var messages = new Dictionary <DiscordUser, DiscordMessage>(); var loups = _users.Values.Where(_ => _.Role == Role.Werewolf).ToList(); foreach (var loup in loups) { var message = await loup.DiscordDmChannel.SendMessageAsync(embed : embed); messages.Add(loup.DiscordUser, message); await RandomCommands.CreatePollReaction(ctx, message, _users.Count + 1); } ctx.Client.DebugLogger.LogMessage(LogLevel.Info, "NafBot", $"Start collecting Reaction", DateTime.Now); var tasks = new List <Task <ReactionCollectionContext> >(); var interactivity = ctx.Client.GetInteractivityModule(); foreach (var message in messages) { var task = Task.Run(() => interactivity.CollectReactionsAsync(message.Value, _wereWolvesTurn)); tasks.Add(task); } var results = await Task.WhenAll(tasks); ctx.Client.DebugLogger.LogMessage(LogLevel.Info, "NafBot", $"Finish collecting Reaction", DateTime.Now); var dicoResults = new Dictionary <Villager, int>(); foreach (var result in results) { foreach (var resultReaction in result.Reactions) { var user = _users[resultReaction.Key]; if (user == null) { continue; } if (dicoResults.ContainsKey(user)) { dicoResults[user] = dicoResults[user] + resultReaction.Value; } else { dicoResults.Add(user, resultReaction.Value); } } } var killedUser = dicoResults.OrderByDescending(_ => _.Value).Take(1).Select(_ => _.Key).First(); await ctx.RespondAsync($"{killedUser.Name} a été choisi pour mourir :)"); return(killedUser); }
public async Task WhoOfUs(CommandContext ctx, string title, params string[] options) { var users = ctx.Guild.Members.Where(_ => _.PermissionsIn(ctx.Channel) == Permissions.AccessChannels); var responseDictionary = new Dictionary <DiscordEmoji, string>(); for (int i = 0; i < options.Length; i++) { responseDictionary.Add(DiscordEmoji.FromName(ctx.Client, $":{Tools.Tools.IntToEmoji(i + 1)}:"), options[i]); } var baseTitle = "[Qdn]" + title; var embed = RandomCommands.CreateEmbed(baseTitle, false, options); var messages = new Dictionary <DiscordUser, DiscordMessage>(); foreach (var user in users) { var dmChannel = await user.CreateDmChannelAsync(); var message = await dmChannel.SendMessageAsync(embed : embed); messages.Add(user, message); await RandomCommands.CreatePollReaction(ctx, message, options.Length + 1); } ctx.Client.DebugLogger.LogMessage(LogLevel.Info, "NafBot", $"Start collecting Reaction", DateTime.Now); var tasks = new List <Task <ReactionCollectionContext> >(); var interactivity = ctx.Client.GetInteractivityModule(); foreach (var message in messages) { var task = Task.Run(() => interactivity.CollectReactionsAsync(message.Value, _responseTime)); tasks.Add(task); } var results = await Task.WhenAll(tasks); ctx.Client.DebugLogger.LogMessage(LogLevel.Info, "NafBot", $"Finish collecting Reaction", DateTime.Now); var dicoResults = new Dictionary <string, int>(); foreach (var result in results) { foreach (var resultReaction in result.Reactions) { var option = responseDictionary[resultReaction.Key]; if (option == null) { continue; } if (dicoResults.ContainsKey(option)) { dicoResults[option] = dicoResults[option] + resultReaction.Value; } else { dicoResults.Add(option, resultReaction.Value); } } } }