Display voter list in new temporary channel

This commit is contained in:
Noikoio 2019-01-31 21:40:45 -08:00
parent fd35a3aa78
commit 762950062d
3 changed files with 72 additions and 52 deletions

View file

@ -1,10 +1,10 @@
using Discord; using Discord;
using Discord.Rest; using Discord.Rest;
using Discord.WebSocket; using Discord.WebSocket;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem; using Noikoio.RegexBot.ConfigItem;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -64,6 +64,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
bool voteIsInitial = false; bool voteIsInitial = false;
string voteCountString = null; string voteCountString = null;
bool voteThresholdReached = false; bool voteThresholdReached = false;
IEnumerable<ulong> finalVoters = null; // used only after channel creation
lock (info) lock (info)
{ {
if (info.GetTemporaryChannel(guild) != null) return; // channel exists, do nothing if (info.GetTemporaryChannel(guild) != null) return; // channel exists, do nothing
@ -88,6 +89,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
if (voteThresholdReached) if (voteThresholdReached)
{ {
info.TempChannelLastActivity = DateTime.UtcNow; info.TempChannelLastActivity = DateTime.UtcNow;
finalVoters = info.Voting.VoterList;
info.Voting.Reset(); info.Voting.Reset();
} }
} }
@ -120,6 +122,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
await newChannel.SendMessageAsync($"Welcome to <#{newChannel.Id}>!" await newChannel.SendMessageAsync($"Welcome to <#{newChannel.Id}>!"
+ "\nBe aware that this channel is temporary and **will** be deleted later."); + "\nBe aware that this channel is temporary and **will** be deleted later.");
await SendVoterList(guild, newChannel, finalVoters);
newChannelId = newChannel.Id; newChannelId = newChannel.Id;
} }
if (voteIsInitial && !voteThresholdReached) if (voteIsInitial && !voteThresholdReached)
@ -147,83 +150,96 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
var newChannel = await g.CreateTextChannelAsync(newChannelName); // exceptions here are handled by caller 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 * 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 * 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 * with the assumption that they already have permissions anyway. Individual users will be assigned
* channel overrides. * channel overrides.
*/ */
if (!newChannelModlist.IsEmpty())
{
await newChannel.SendMessageAsync("Applying permissions...");
}
foreach (var item in newChannelModlist.Roles) foreach (var item in newChannelModlist.Roles)
{ {
// Evaluate role from data // Evaluate role from data
SocketRole r = null; SocketRole r = null;
if (item.Id.HasValue) r = g.GetRole(item.Id.Value); if (item.Id.HasValue) r = g.GetRole(item.Id.Value);
if (r == null && item.Name != null) if (r == null && item.Name != null)
{ {
r = g.Roles.FirstOrDefault(gr => string.Equals(gr.Name, item.Name, StringComparison.OrdinalIgnoreCase)); r = g.Roles.FirstOrDefault(gr => string.Equals(gr.Name, item.Name, StringComparison.OrdinalIgnoreCase));
} }
if (r == null) 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; continue;
} }
try try
{ {
await newChannel.AddPermissionOverwriteAsync(r, new OverwritePermissions( await newChannel.AddPermissionOverwriteAsync(r, new OverwritePermissions(
manageChannel: PermValue.Allow, manageChannel: PermValue.Allow,
sendMessages: PermValue.Allow, sendMessages: PermValue.Allow,
manageMessages: PermValue.Allow, manageMessages: PermValue.Allow,
managePermissions: PermValue.Allow)); managePermissions: PermValue.Allow));
} }
catch (Discord.Net.HttpException ex) catch (Discord.Net.HttpException ex)
{ {
// TODO what error code are we looking for here? adjust to pick up only that. // 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 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}"); await newChannel.SendMessageAsync($":x: Unable to set on `{r.Name}`: {ex.Message}");
} }
} }
foreach (var item in newChannelModlist.Users) foreach (var item in newChannelModlist.Users)
{ {
// Evaluate user from data // Evaluate user from data
SocketUser u = null; SocketUser u = null;
if (item.Id.HasValue) u = g.GetUser(item.Id.Value); if (item.Id.HasValue) u = g.GetUser(item.Id.Value);
if (u == null && item.Name != null) if (u == null && item.Name != null)
{ {
u = g.Users.FirstOrDefault(gu => string.Equals(gu.Username, item.Name, StringComparison.OrdinalIgnoreCase)); u = g.Users.FirstOrDefault(gu => string.Equals(gu.Username, item.Name, StringComparison.OrdinalIgnoreCase));
} }
if (u == null) 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; continue;
} }
try try
{ {
await newChannel.AddPermissionOverwriteAsync(u, new OverwritePermissions( await newChannel.AddPermissionOverwriteAsync(u, new OverwritePermissions(
manageChannel: PermValue.Allow, manageChannel: PermValue.Allow,
sendMessages: PermValue.Allow, sendMessages: PermValue.Allow,
manageMessages: PermValue.Allow, manageMessages: PermValue.Allow,
managePermissions: PermValue.Allow)); managePermissions: PermValue.Allow));
} }
catch (Discord.Net.HttpException ex) 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}"); 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; return newChannel;
} }
private async Task SendVoterList(SocketGuild g, RestTextChannel ch, IEnumerable<ulong> 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());
}
/// <summary> /// <summary>
/// Listens for any message sent to the temporary channel. /// Listens for any message sent to the temporary channel.
/// Updates the corresponding internal value. /// Updates the corresponding internal value.
@ -259,7 +275,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
{ {
try { await Task.Delay(12000, _backgroundWorkerCancel.Token); } try { await Task.Delay(12000, _backgroundWorkerCancel.Token); }
catch (TaskCanceledException) { return; } catch (TaskCanceledException) { return; }
foreach (var g in Client.Guilds) foreach (var g in Client.Guilds)
{ {
try try
@ -298,7 +314,8 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
{ {
bool act; bool act;
string nameTest; string nameTest;
lock (info) { lock (info)
{
act = info.Voting.IsSessionExpired(); act = info.Voting.IsSessionExpired();
nameTest = info.Config.VoteChannel; nameTest = info.Config.VoteChannel;
if (act) info.Voting.StartCooldown(); if (act) info.Voting.StartCooldown();

View file

@ -47,6 +47,11 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
return _votes.Count == 0; return _votes.Count == 0;
} }
/// <summary>
/// Returns the list of users who issued a vote.
/// </summary>
public IEnumerable<ulong> VoterList => _votes;
/// <summary> /// <summary>
/// Checks if the voting session has expired. /// Checks if the voting session has expired.
/// To be called by the background task. This automatically resets and sets cooldown. /// To be called by the background task. This automatically resets and sets cooldown.
@ -80,7 +85,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
/// </summary> /// </summary>
public void Reset() public void Reset()
{ {
_votes.Clear(); _votes = new List<ulong>();
_cooldownStart = null; _cooldownStart = null;
} }
} }

View file

@ -4,12 +4,10 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
<RootNamespace>Noikoio.RegexBot</RootNamespace> <RootNamespace>Noikoio.RegexBot</RootNamespace>
<AssemblyVersion>2.6.3</AssemblyVersion>
<Description>Highly configurable Discord moderation bot</Description> <Description>Highly configurable Discord moderation bot</Description>
<Authors>Noikoio</Authors> <Authors>Noikoio</Authors>
<Company /> <Company />
<FileVersion>2.6.2</FileVersion> <Version>2.6.4</Version>
<Version>2.6.3</Version>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">