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.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<ulong> 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<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>
/// 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();

View file

@ -47,6 +47,11 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
return _votes.Count == 0;
}
/// <summary>
/// Returns the list of users who issued a vote.
/// </summary>
public IEnumerable<ulong> VoterList => _votes;
/// <summary>
/// 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
/// </summary>
public void Reset()
{
_votes.Clear();
_votes = new List<ulong>();
_cooldownStart = null;
}
}

View file

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