From 762950062d57950ce8c43ea6aa5deb789ccdff35 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Thu, 31 Jan 2019 21:40:45 -0800 Subject: [PATCH] Display voter list in new temporary channel --- .../Module/VoteTempChannel/VoteTempChannel.cs | 113 ++++++++++-------- .../Module/VoteTempChannel/VotingSession.cs | 7 +- RegexBot/RegexBot.csproj | 4 +- 3 files changed, 72 insertions(+), 52 deletions(-) diff --git a/RegexBot/Module/VoteTempChannel/VoteTempChannel.cs b/RegexBot/Module/VoteTempChannel/VoteTempChannel.cs index 55db2fa..f4b375d 100644 --- a/RegexBot/Module/VoteTempChannel/VoteTempChannel.cs +++ b/RegexBot/Module/VoteTempChannel/VoteTempChannel.cs @@ -1,10 +1,10 @@ -using Discord; +using Discord; using Discord.Rest; using Discord.WebSocket; using Newtonsoft.Json.Linq; using Noikoio.RegexBot.ConfigItem; using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -64,6 +64,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel bool voteIsInitial = false; string voteCountString = null; bool voteThresholdReached = false; + IEnumerable finalVoters = null; // used only after channel creation lock (info) { if (info.GetTemporaryChannel(guild) != null) return; // channel exists, do nothing @@ -88,6 +89,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel if (voteThresholdReached) { info.TempChannelLastActivity = DateTime.UtcNow; + finalVoters = info.Voting.VoterList; info.Voting.Reset(); } } @@ -120,6 +122,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel await newChannel.SendMessageAsync($"Welcome to <#{newChannel.Id}>!" + "\nBe aware that this channel is temporary and **will** be deleted later."); + await SendVoterList(guild, newChannel, finalVoters); newChannelId = newChannel.Id; } if (voteIsInitial && !voteThresholdReached) @@ -147,83 +150,96 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel var newChannel = await g.CreateTextChannelAsync(newChannelName); // exceptions here are handled by caller - /* - * Here we attempt to set permissions on the temporary channel. We will attempt all items within - * the Roles and Users sections of the EntityList. Roles that are above us will not be processed - * with the assumption that they already have permissions anyway. Individual users will be assigned - * channel overrides. - */ - - if (!newChannelModlist.IsEmpty()) - { - await newChannel.SendMessageAsync("Applying permissions..."); - } + /* + * Here we attempt to set permissions on the temporary channel. We will attempt all items within + * the Roles and Users sections of the EntityList. Roles that are above us will not be processed + * with the assumption that they already have permissions anyway. Individual users will be assigned + * channel overrides. + */ + foreach (var item in newChannelModlist.Roles) - { - // Evaluate role from data - SocketRole r = null; - if (item.Id.HasValue) r = g.GetRole(item.Id.Value); + { + // Evaluate role from data + SocketRole r = null; + if (item.Id.HasValue) r = g.GetRole(item.Id.Value); if (r == null && item.Name != null) { r = g.Roles.FirstOrDefault(gr => string.Equals(gr.Name, item.Name, StringComparison.OrdinalIgnoreCase)); - } + } if (r == null) { - await newChannel.SendMessageAsync($"Unable to find role `{item.ToString()}` to apply permissions."); + await newChannel.SendMessageAsync($"Unable to find role `{item.ToString()}` to apply permissions."); continue; - } + } try { - await newChannel.AddPermissionOverwriteAsync(r, new OverwritePermissions( - manageChannel: PermValue.Allow, - sendMessages: PermValue.Allow, - manageMessages: PermValue.Allow, + await newChannel.AddPermissionOverwriteAsync(r, new OverwritePermissions( + manageChannel: PermValue.Allow, + sendMessages: PermValue.Allow, + manageMessages: PermValue.Allow, managePermissions: PermValue.Allow)); - } + } catch (Discord.Net.HttpException ex) { - // TODO what error code are we looking for here? adjust to pick up only that. - // TODO clean up the error message display. users don't want to see the gory details. + // TODO what error code are we looking for here? adjust to pick up only that. + // TODO clean up the error message display. users don't want to see the gory details. await newChannel.SendMessageAsync($":x: Unable to set on `{r.Name}`: {ex.Message}"); } - } + } foreach (var item in newChannelModlist.Users) { - // Evaluate user from data - SocketUser u = null; - if (item.Id.HasValue) u = g.GetUser(item.Id.Value); + // Evaluate user from data + SocketUser u = null; + if (item.Id.HasValue) u = g.GetUser(item.Id.Value); if (u == null && item.Name != null) { u = g.Users.FirstOrDefault(gu => string.Equals(gu.Username, item.Name, StringComparison.OrdinalIgnoreCase)); - } + } if (u == null) { - await newChannel.SendMessageAsync($"Unable to find user `{item.ToString()}` to apply permissions."); + await newChannel.SendMessageAsync($"Unable to find user `{item.ToString()}` to apply permissions."); continue; - } - + } + try { - await newChannel.AddPermissionOverwriteAsync(u, new OverwritePermissions( - manageChannel: PermValue.Allow, - sendMessages: PermValue.Allow, - manageMessages: PermValue.Allow, + await newChannel.AddPermissionOverwriteAsync(u, new OverwritePermissions( + manageChannel: PermValue.Allow, + sendMessages: PermValue.Allow, + manageMessages: PermValue.Allow, managePermissions: PermValue.Allow)); - } + } catch (Discord.Net.HttpException ex) { - // TODO same as above. which code do we want to catch specifically? + // TODO same as above. which code do we want to catch specifically? await newChannel.SendMessageAsync($":x: Unable to set on `{u.Username}`: {ex.Message}"); } - } - if (!newChannelModlist.IsEmpty()) - { - await newChannel.SendMessageAsync("Permission application process has completed."); - } + } return newChannel; } + private async Task SendVoterList(SocketGuild g, RestTextChannel ch, IEnumerable voters) + { + var names = new System.Text.StringBuilder(); + foreach (var item in voters) + { + var u = g.GetUser(item); + if (u == null) + { + names.AppendLine("Unknown user with ID " + item); + } + else + { + names.Append(u.Username + "#" + u.Discriminator); + if (u.Nickname != null) names.Append(" (" + u.Nickname + ")"); + names.AppendLine(); + } + } + + await ch.SendMessageAsync("Persons who voted to create this channel:\n" + names.ToString()); + } + /// /// Listens for any message sent to the temporary channel. /// Updates the corresponding internal value. @@ -259,7 +275,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel { try { await Task.Delay(12000, _backgroundWorkerCancel.Token); } catch (TaskCanceledException) { return; } - + foreach (var g in Client.Guilds) { try @@ -298,7 +314,8 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel { bool act; string nameTest; - lock (info) { + lock (info) + { act = info.Voting.IsSessionExpired(); nameTest = info.Config.VoteChannel; if (act) info.Voting.StartCooldown(); diff --git a/RegexBot/Module/VoteTempChannel/VotingSession.cs b/RegexBot/Module/VoteTempChannel/VotingSession.cs index 45c5a35..aa1cac8 100644 --- a/RegexBot/Module/VoteTempChannel/VotingSession.cs +++ b/RegexBot/Module/VoteTempChannel/VotingSession.cs @@ -47,6 +47,11 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel return _votes.Count == 0; } + /// + /// Returns the list of users who issued a vote. + /// + public IEnumerable VoterList => _votes; + /// /// Checks if the voting session has expired. /// To be called by the background task. This automatically resets and sets cooldown. @@ -80,7 +85,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel /// public void Reset() { - _votes.Clear(); + _votes = new List(); _cooldownStart = null; } } diff --git a/RegexBot/RegexBot.csproj b/RegexBot/RegexBot.csproj index bf633df..c9a60e8 100644 --- a/RegexBot/RegexBot.csproj +++ b/RegexBot/RegexBot.csproj @@ -4,12 +4,10 @@ Exe netcoreapp2.0 Noikoio.RegexBot - 2.6.3 Highly configurable Discord moderation bot Noikoio - 2.6.2 - 2.6.3 + 2.6.4