internal async Task <VotingInfo> StartVoting(string description, TimeSpan duration, Dictionary <ulong, string> answers, SocketTextChannel channel, IUser author) { var guild = Bot.Instance.ServiceManager.GetService <SingleServerInfoProviderService>().Guild; var voting = new VotingInfo() { Answers = answers, Begin = DateTime.Now, Description = description, End = DateTime.Now + duration, BeginMessageChannelId = channel.Id, }; var embedBuilder = new EmbedBuilder(); var answersText = new StringBuilder(); foreach (var answer in answers) { var emote = await guild.GetEmoteAsync(answer.Key); answersText.AppendLine($"{emote} - {answer.Value}"); } embedBuilder .WithTimestamp(DateTime.Now + duration) .WithDescription(description) .AddField(this.Config.BeginMessageAnswersTitle, answersText.ToString()); if (this.Config.ShowAuthor) { embedBuilder.WithAuthor(author); } if (!string.IsNullOrEmpty(this.Config.BeginMessageTitle)) { embedBuilder.WithTitle(this.Config.BeginMessageTitle); } if (!string.IsNullOrEmpty(this.Config.BeginMessageFooter)) { embedBuilder.WithFooter(this.Config.BeginMessageFooter.Replace("{duration}", duration.ToString())); } var message = await channel.SendMessageAsync(null, false, embedBuilder.Build()); foreach (var reaction in answers.Keys) { await message.AddReactionAsync(await guild.GetEmoteAsync(reaction)); } voting.BeginMessageId = message.Id; this.Config.ActiveVotings.Add(voting); return(voting); }
private async Task SendWinMessageAsync(VotingInfo voting) { var channel = Bot.Instance.ServiceManager.GetService <SingleServerInfoProviderService>().Guild.GetTextChannel(voting.BeginMessageChannelId); var message = await channel.GetMessageAsync(voting.BeginMessageId); if (message is null) { return; } var winnerEmote = (Emote)message.Reactions.OrderByDescending(reaction => { if (reaction.Key is Emote emote) { if (voting.Answers.ContainsKey(emote.Id)) { return(reaction.Value.ReactionCount); } } return(-1); }).First().Key; var embedBuilder = new EmbedBuilder() .WithCurrentTimestamp() .WithDescription(voting.Description) .AddField(this.Config.EndMessageBeginMessageJumpUrlTitle, message.GetJumpUrl()) .AddField(this.Config.EndMessageResultTitle, this.Config.EndMessageResultBody.Replace("{winnerEmote}", winnerEmote.ToString()).Replace("{winnerAnswer}", voting.Answers[winnerEmote.Id])); if (this.Config.ShowAuthor) { var messageAuthor = message.Embeds.First().Author; var author = new EmbedAuthorBuilder() { Name = messageAuthor.Value.Name, Url = messageAuthor.Value.Url, IconUrl = messageAuthor.Value.IconUrl, }; embedBuilder.WithAuthor(author); } if (!string.IsNullOrEmpty(this.Config.EndMessageFooter)) { embedBuilder.WithFooter(this.Config.EndMessageFooter); } if (!string.IsNullOrEmpty(this.Config.EndMessageTitle)) { embedBuilder.WithTitle(this.Config.EndMessageTitle); } await channel.SendMessageAsync(null, false, embedBuilder.Build()); }