From 19cf4d890fdd54e1bb1a6098fc7c5e066398fe63 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Wed, 31 Oct 2018 13:11:12 -0700 Subject: [PATCH] Minor fixes and modifications --- Module/VoteTempChannel/Configuration.cs | 44 +++++++-------- Module/VoteTempChannel/GuildInformation.cs | 2 +- Module/VoteTempChannel/VoteTempChannel.cs | 62 +++++++++++----------- Module/VoteTempChannel/VotingSession.cs | 3 +- 4 files changed, 54 insertions(+), 57 deletions(-) diff --git a/Module/VoteTempChannel/Configuration.cs b/Module/VoteTempChannel/Configuration.cs index d321aab..4bb4ce2 100644 --- a/Module/VoteTempChannel/Configuration.cs +++ b/Module/VoteTempChannel/Configuration.cs @@ -7,10 +7,27 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel { class Configuration { + /// + /// Channel name in which voting takes place. + /// + public string VoteChannel { get; } /// /// Command used to vote for the channel's creation. /// public string VoteCommand { get; } + /// + /// Number of votes needed to create the channel. + /// + public int VotePassThreshold { get; } + /// + /// Amount of time that a voting session can last starting from its initial vote. + /// + public TimeSpan VotingDuration { get; } + /// + /// Amount of time to wait before another vote may be initiated, either after a failed vote + /// or from expiration of the temporary channel. + /// + public TimeSpan VotingCooldown { get; } /// /// Name of the temporary channel, without prefix. @@ -22,27 +39,6 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel /// public TimeSpan ChannelDuration { get; } - /// - /// Number of votes needed to create the channel. - /// - public int VotePassThreshold { get; } - - /// - /// Amount of time that a voting session can last starting from its initial vote. - /// - public TimeSpan VotingDuration { get; } - - /// - /// Amount of time to wait before another vote may be initiated, either after a failed vote - /// or from expiration of the temporary channel. - /// - public TimeSpan VotingCooldown { get; } - - /// - /// Channel name in which voting takes place. - /// - public string VotingChannel { get; } - public Configuration(JObject j) { VoteCommand = j["VoteCommand"]?.Value(); @@ -52,7 +48,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel throw new RuleImportException("'VoteCommand' must not contain spaces."); TempChannelName = ParseChannelNameConfig(j, "TempChannelName"); - VotingChannel = ParseChannelNameConfig(j, "VotingChannel"); + VoteChannel = ParseChannelNameConfig(j, "VoteChannel"); var vptProp = j["VotePassThreshold"]; if (vptProp == null) @@ -70,10 +66,10 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel private string ParseChannelNameConfig(JObject conf, string valueName) { - var value = j[valueName]?.Value(); + var value = conf[valueName]?.Value(); if (string.IsNullOrWhiteSpace(value)) throw new RuleImportException($"'{valueName}' must be specified."); - if (!Regex.IsMatch(TempChannelName, @"^([A-Za-z0-9]|[-_ ])+$")) + if (!Regex.IsMatch(value, @"^([A-Za-z0-9]|[-_ ])+$")) throw new RuleImportException($"'{valueName}' contains one or more invalid characters."); return value; } diff --git a/Module/VoteTempChannel/GuildInformation.cs b/Module/VoteTempChannel/GuildInformation.cs index 90362d8..28164d0 100644 --- a/Module/VoteTempChannel/GuildInformation.cs +++ b/Module/VoteTempChannel/GuildInformation.cs @@ -25,7 +25,7 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel TempChannelLastActivity = DateTimeOffset.UtcNow; Config = new Configuration(conf); - Voting = new VotingSession(); + Voting = new VotingSession(Config); } public SocketTextChannel GetTemporaryChannel(SocketGuild guild) diff --git a/Module/VoteTempChannel/VoteTempChannel.cs b/Module/VoteTempChannel/VoteTempChannel.cs index 9f86815..f17d593 100644 --- a/Module/VoteTempChannel/VoteTempChannel.cs +++ b/Module/VoteTempChannel/VoteTempChannel.cs @@ -1,5 +1,4 @@ -using Discord; -using Discord.Rest; +using Discord.Rest; using Discord.WebSocket; using Newtonsoft.Json.Linq; using Noikoio.RegexBot.ConfigItem; @@ -46,35 +45,35 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel if (arg.Author.IsBot) return; var guild = (arg.Channel as SocketTextChannel)?.Guild; if (guild == null) return; - var conf = GetState(guild.Id); - if (conf == null) return; + var info = GetState(guild.Id); + if (info == null) return; // Only check the designated voting channel - if (!string.Equals(arg.Channel.Name, conf.Config.VotingChannel, + if (!string.Equals(arg.Channel.Name, info.Config.VoteChannel, StringComparison.InvariantCultureIgnoreCase)) return; // Check if command invoked - if (!arg.Content.StartsWith(conf.Config.VoteCommand, StringComparison.InvariantCultureIgnoreCase)) return; + if (!arg.Content.StartsWith(info.Config.VoteCommand, StringComparison.InvariantCultureIgnoreCase)) return; // Check if we're accepting votes. Locking here; background task may be using this. bool cooldown; bool voteCounted = false; string newChannelName = null; - lock (conf) + lock (info) { - if (conf.GetTemporaryChannel(guild) != null) return; // channel exists, nothing to vote for - cooldown = conf.Voting.IsInCooldown(); + if (info.GetTemporaryChannel(guild) != null) return; // channel exists, nothing to vote for + cooldown = info.Voting.IsInCooldown(); if (!cooldown) { - voteCounted = conf.Voting.AddVote(arg.Author.Id, out var voteCount); - if (voteCount >= conf.Config.VotePassThreshold) + voteCounted = info.Voting.AddVote(arg.Author.Id, out var voteCount); + if (voteCount >= info.Config.VotePassThreshold) { - newChannelName = conf.Config.TempChannelName; + newChannelName = info.Config.TempChannelName; } } // Prepare new temporary channel while we're still locking state - if (newChannelName != null) conf.TempChannelLastActivity = DateTime.UtcNow; + if (newChannelName != null) info.TempChannelLastActivity = DateTime.UtcNow; } if (cooldown) @@ -120,15 +119,16 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel if (arg.Author.IsBot) return Task.CompletedTask; var guild = (arg.Channel as SocketTextChannel)?.Guild; if (guild == null) return Task.CompletedTask; - var conf = GetState(guild.Id); - if (conf == null) return Task.CompletedTask; + var info = GetState(guild.Id); + if (info == null) return Task.CompletedTask; - lock (conf) + lock (info) { - var tch = conf.GetTemporaryChannel(guild); + var tch = info.GetTemporaryChannel(guild); + if (tch == null) return Task.CompletedTask; if (arg.Channel.Name == tch.Name) { - conf.TempChannelLastActivity = DateTimeOffset.UtcNow; + info.TempChannelLastActivity = DateTimeOffset.UtcNow; } } @@ -150,11 +150,11 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel { try { - var conf = GetState(g.Id); - if (conf == null) continue; + var info = GetState(g.Id); + if (info == null) continue; - await BackgroundTempChannelExpiryCheck(g, conf); - await BackgroundVoteSessionExpiryCheck(g, conf); + await BackgroundTempChannelExpiryCheck(g, info); + await BackgroundVoteSessionExpiryCheck(g, info); } catch (Exception ex) { @@ -165,28 +165,28 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel } } - private async Task BackgroundTempChannelExpiryCheck(SocketGuild g, GuildInformation conf) + private async Task BackgroundTempChannelExpiryCheck(SocketGuild g, GuildInformation info) { SocketGuildChannel ch = null; - lock (conf) + lock (info) { - ch = conf.GetTemporaryChannel(g); + ch = info.GetTemporaryChannel(g); if (ch == null) return; // No temporary channel. Nothing to do. - if (!conf.IsTempChannelExpired()) return; + if (!info.IsTempChannelExpired()) return; // If we got this far, the channel's expiring. Start the voting cooldown. - conf.Voting.StartCooldown(); + info.Voting.StartCooldown(); } await ch.DeleteAsync(); } - private async Task BackgroundVoteSessionExpiryCheck(SocketGuild g, GuildInformation conf) + private async Task BackgroundVoteSessionExpiryCheck(SocketGuild g, GuildInformation info) { bool act; string nameTest; - lock (conf) { - act = conf.Voting.IsSessionExpired(); - nameTest = conf.Config.VotingChannel; + lock (info) { + act = info.Voting.IsSessionExpired(); + nameTest = info.Config.VoteChannel; } if (!act) return; diff --git a/Module/VoteTempChannel/VotingSession.cs b/Module/VoteTempChannel/VotingSession.cs index cd2dbdb..e488243 100644 --- a/Module/VoteTempChannel/VotingSession.cs +++ b/Module/VoteTempChannel/VotingSession.cs @@ -13,8 +13,9 @@ namespace Noikoio.RegexBot.Module.VoteTempChannel private List _votes; public DateTimeOffset? _cooldownStart; - public VotingSession() + public VotingSession(Configuration conf) { + _conf = conf; _cooldownStart = null; _votes = new List(); }