Add initial vote whitelist

This commit is contained in:
Noikoio 2018-10-31 14:29:08 -07:00
parent 19cf4d890f
commit 607f56bcd0
4 changed files with 38 additions and 4 deletions

View file

@ -123,5 +123,13 @@ namespace Noikoio.RegexBot.ConfigItem
// No match.
return false;
}
/// <summary>
/// Determines if this is an empty list.
/// </summary>
public bool IsEmpty()
{
return Channels.Count() + Roles.Count() + Users.Count() == 0;
}
}
}

View file

@ -1,6 +1,7 @@
using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem;
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Noikoio.RegexBot.Module.VoteTempChannel
@ -39,6 +40,12 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
/// </summary>
public TimeSpan ChannelDuration { get; }
/// <summary>
/// Entities that are allowed to cast the initial vote.
/// An empty list allows anyone to initiate a vote.
/// </summary>
public EntityList VoteStarters { get; }
public Configuration(JObject j)
{
VoteCommand = j["VoteCommand"]?.Value<string>();
@ -62,6 +69,9 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
ChannelDuration = ParseTimeConfig(j, "ChannelDuration");
VotingDuration = ParseTimeConfig(j, "VotingDuration");
VotingCooldown = ParseTimeConfig(j, "VotingCooldown");
VoteStarters = new EntityList(j["VoteStarters"]);
// Accepts empty lists.
}
private string ParseChannelNameConfig(JObject conf, string valueName)

View file

@ -55,13 +55,21 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
// Check if command invoked
if (!arg.Content.StartsWith(info.Config.VoteCommand, StringComparison.InvariantCultureIgnoreCase)) return;
// Check if we're accepting votes. Locking here; background task may be using this.
// Check if we're accepting votes. Locking here; other tasks may alter this data.
bool cooldown;
bool voteCounted = false;
string newChannelName = null;
lock (info)
{
if (info.GetTemporaryChannel(guild) != null) return; // channel exists, nothing to vote for
if (info.GetTemporaryChannel(guild) != null) return; // channel exists, do nothing
if (info.Voting.AwaitingInitialVote())
{
// Vote not in effect. Ignore those not allowed to initiate a vote (if configured).
if (!info.Config.VoteStarters.IsEmpty() &&
!info.Config.VoteStarters.ExistsInList(arg)) return;
}
cooldown = info.Voting.IsInCooldown();
if (!cooldown)
{
@ -72,7 +80,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
}
}
// Prepare new temporary channel while we're still locking state
// Prepare new temporary channel while we're still locking
if (newChannelName != null) info.TempChannelLastActivity = DateTime.UtcNow;
}
@ -105,7 +113,6 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
+ "\nBe aware that this channel is temporary and **will** be deleted later.");
newChannelName = newCh.Id.ToString(); // For use in the confirmation message
}
await arg.Channel.SendMessageAsync(":white_check_mark: Channel creation vote has been counted."
+ (newChannelName != null ? $"\n<#{newChannelName}> has been created!" : ""));
}

View file

@ -38,6 +38,15 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel
return true;
}
/// <summary>
/// Reports if this session is awaiting its first vote.
/// </summary>
public bool AwaitingInitialVote()
{
if (IsInCooldown()) return false;
return _votes.Count == 0;
}
/// <summary>
/// Checks if the voting session has expired.
/// To be called by the background task. This automatically resets and sets cooldown.