2018-10-31 20:11:12 +00:00
|
|
|
|
using Discord.Rest;
|
2018-10-28 18:44:30 +00:00
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Noikoio.RegexBot.ConfigItem;
|
|
|
|
|
using System;
|
2018-10-29 02:53:56 +00:00
|
|
|
|
using System.Threading;
|
2018-10-28 18:44:30 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Noikoio.RegexBot.Module.VoteTempChannel
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2018-10-29 04:09:17 +00:00
|
|
|
|
/// Enables users to vote for the creation of a temporary channel.
|
|
|
|
|
/// Deletes the channel after a set period of inactivity.
|
2018-10-28 18:44:30 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
class VoteTempChannel : BotModule
|
|
|
|
|
{
|
2018-10-29 02:53:56 +00:00
|
|
|
|
Task _backgroundWorker;
|
|
|
|
|
CancellationTokenSource _backgroundWorkerCancel;
|
2018-10-28 18:44:30 +00:00
|
|
|
|
|
|
|
|
|
public VoteTempChannel(DiscordSocketClient client) : base(client)
|
|
|
|
|
{
|
2018-10-29 04:09:17 +00:00
|
|
|
|
client.MessageReceived += VoteChecking;
|
|
|
|
|
client.MessageReceived += TemporaryChannelActivityCheck;
|
2018-10-29 02:53:56 +00:00
|
|
|
|
|
|
|
|
|
_backgroundWorkerCancel = new CancellationTokenSource();
|
|
|
|
|
_backgroundWorker = Task.Factory.StartNew(BackgroundCheckingTask, _backgroundWorkerCancel.Token,
|
|
|
|
|
TaskCreationOptions.LongRunning, TaskScheduler.Default);
|
2018-10-28 18:44:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 04:09:17 +00:00
|
|
|
|
public override Task<object> CreateInstanceState(JToken configSection)
|
2018-10-28 18:44:30 +00:00
|
|
|
|
{
|
2018-10-29 04:09:17 +00:00
|
|
|
|
if (configSection == null) return Task.FromResult<object>(null);
|
|
|
|
|
if (configSection.Type == JTokenType.Object)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult<object>(new GuildInformation((JObject)configSection));
|
|
|
|
|
}
|
|
|
|
|
throw new RuleImportException("Configuration not of a valid type.");
|
2018-10-28 18:44:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 04:09:17 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Listens for voting commands.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async Task VoteChecking(SocketMessage arg)
|
2018-10-28 18:44:30 +00:00
|
|
|
|
{
|
|
|
|
|
if (arg.Author.IsBot) return;
|
|
|
|
|
var guild = (arg.Channel as SocketTextChannel)?.Guild;
|
|
|
|
|
if (guild == null) return;
|
2018-10-31 20:11:12 +00:00
|
|
|
|
var info = GetState<GuildInformation>(guild.Id);
|
|
|
|
|
if (info == null) return;
|
2018-10-28 18:44:30 +00:00
|
|
|
|
|
2018-10-29 04:09:17 +00:00
|
|
|
|
// Only check the designated voting channel
|
2018-10-31 20:11:12 +00:00
|
|
|
|
if (!string.Equals(arg.Channel.Name, info.Config.VoteChannel,
|
2018-10-29 04:09:17 +00:00
|
|
|
|
StringComparison.InvariantCultureIgnoreCase)) return;
|
2018-10-28 18:44:30 +00:00
|
|
|
|
|
2018-10-29 04:09:17 +00:00
|
|
|
|
// Check if command invoked
|
2018-10-31 20:11:12 +00:00
|
|
|
|
if (!arg.Content.StartsWith(info.Config.VoteCommand, StringComparison.InvariantCultureIgnoreCase)) return;
|
2018-10-29 04:09:17 +00:00
|
|
|
|
|
2018-10-31 21:29:08 +00:00
|
|
|
|
// Check if we're accepting votes. Locking here; other tasks may alter this data.
|
2018-10-29 04:09:17 +00:00
|
|
|
|
bool cooldown;
|
|
|
|
|
bool voteCounted = false;
|
2018-11-26 02:02:40 +00:00
|
|
|
|
bool voteIsInitial = false;
|
2018-10-29 04:09:17 +00:00
|
|
|
|
string newChannelName = null;
|
2018-10-31 20:11:12 +00:00
|
|
|
|
lock (info)
|
2018-10-29 04:09:17 +00:00
|
|
|
|
{
|
2018-10-31 21:29:08 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:11:12 +00:00
|
|
|
|
cooldown = info.Voting.IsInCooldown();
|
2018-10-29 04:09:17 +00:00
|
|
|
|
if (!cooldown)
|
|
|
|
|
{
|
2018-10-31 20:11:12 +00:00
|
|
|
|
voteCounted = info.Voting.AddVote(arg.Author.Id, out var voteCount);
|
2018-11-26 02:02:40 +00:00
|
|
|
|
voteIsInitial = voteCount == 1;
|
2018-10-31 20:11:12 +00:00
|
|
|
|
if (voteCount >= info.Config.VotePassThreshold)
|
2018-10-29 04:09:17 +00:00
|
|
|
|
{
|
2018-11-26 02:02:40 +00:00
|
|
|
|
// Non-null value in newChannelName signals vote success
|
2018-10-31 20:11:12 +00:00
|
|
|
|
newChannelName = info.Config.TempChannelName;
|
2018-10-29 04:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-26 02:02:40 +00:00
|
|
|
|
// Prepare some stuff while we're still in the lock
|
|
|
|
|
if (newChannelName != null)
|
|
|
|
|
{
|
|
|
|
|
info.TempChannelLastActivity = DateTime.UtcNow;
|
|
|
|
|
info.Voting.Reset();
|
|
|
|
|
}
|
2018-10-29 04:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cooldown)
|
2018-10-28 18:44:30 +00:00
|
|
|
|
{
|
|
|
|
|
await arg.Channel.SendMessageAsync(":x: Cooldown in effect. Try again later.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-29 04:09:17 +00:00
|
|
|
|
if (!voteCounted)
|
2018-10-28 18:44:30 +00:00
|
|
|
|
{
|
2018-10-29 04:09:17 +00:00
|
|
|
|
await arg.Channel.SendMessageAsync(":x: You have already voted.");
|
|
|
|
|
return;
|
2018-10-28 18:44:30 +00:00
|
|
|
|
}
|
2018-10-29 04:09:17 +00:00
|
|
|
|
|
|
|
|
|
if (newChannelName != null)
|
2018-10-28 18:44:30 +00:00
|
|
|
|
{
|
2018-10-29 04:09:17 +00:00
|
|
|
|
RestTextChannel newCh;
|
|
|
|
|
try
|
2018-10-28 18:44:30 +00:00
|
|
|
|
{
|
2018-10-29 04:09:17 +00:00
|
|
|
|
newCh = await guild.CreateTextChannelAsync(newChannelName);
|
|
|
|
|
}
|
|
|
|
|
catch (Discord.Net.HttpException ex)
|
|
|
|
|
{
|
|
|
|
|
await Log($"Failed to create temporary channel: {ex.Message}");
|
|
|
|
|
await arg.Channel.SendMessageAsync(":x: Failed to create new channel! Notify the bot operator.");
|
2018-10-28 18:44:30 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 04:09:17 +00:00
|
|
|
|
await newCh.SendMessageAsync($"Welcome to <#{newCh.Id}>!"
|
|
|
|
|
+ "\nBe aware that this channel is temporary and **will** be deleted later.");
|
|
|
|
|
newChannelName = newCh.Id.ToString(); // For use in the confirmation message
|
2018-10-28 18:44:30 +00:00
|
|
|
|
}
|
2018-11-26 02:02:40 +00:00
|
|
|
|
if (voteIsInitial && newChannelName == null)
|
|
|
|
|
await arg.Channel.SendMessageAsync($":white_check_mark: {arg.Author.Mention} has initiated a vote! " +
|
|
|
|
|
"Others may now vote to confirm creation of the new channel.");
|
|
|
|
|
else
|
|
|
|
|
await arg.Channel.SendMessageAsync(":white_check_mark: Channel creation vote has been counted."
|
|
|
|
|
+ (newChannelName != null ? $"\n<#{newChannelName}> is now active!" : ""));
|
2018-10-28 18:44:30 +00:00
|
|
|
|
}
|
2018-10-29 04:09:17 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Listens for any message sent to the temporary channel.
|
|
|
|
|
/// Updates the corresponding internal value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private Task TemporaryChannelActivityCheck(SocketMessage arg)
|
2018-10-28 18:44:30 +00:00
|
|
|
|
{
|
2018-10-29 04:09:17 +00:00
|
|
|
|
if (arg.Author.IsBot) return Task.CompletedTask;
|
|
|
|
|
var guild = (arg.Channel as SocketTextChannel)?.Guild;
|
|
|
|
|
if (guild == null) return Task.CompletedTask;
|
2018-10-31 20:11:12 +00:00
|
|
|
|
var info = GetState<GuildInformation>(guild.Id);
|
|
|
|
|
if (info == null) return Task.CompletedTask;
|
2018-10-28 18:44:30 +00:00
|
|
|
|
|
2018-10-31 20:11:12 +00:00
|
|
|
|
lock (info)
|
2018-10-28 18:44:30 +00:00
|
|
|
|
{
|
2018-10-31 20:11:12 +00:00
|
|
|
|
var tch = info.GetTemporaryChannel(guild);
|
|
|
|
|
if (tch == null) return Task.CompletedTask;
|
2018-10-29 04:09:17 +00:00
|
|
|
|
if (arg.Channel.Name == tch.Name)
|
|
|
|
|
{
|
2018-10-31 20:11:12 +00:00
|
|
|
|
info.TempChannelLastActivity = DateTimeOffset.UtcNow;
|
2018-10-29 04:09:17 +00:00
|
|
|
|
}
|
2018-10-28 18:44:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 04:09:17 +00:00
|
|
|
|
return Task.CompletedTask;
|
2018-10-28 18:44:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 02:53:56 +00:00
|
|
|
|
#region Background tasks
|
2018-10-28 18:44:30 +00:00
|
|
|
|
/// <summary>
|
2018-10-29 02:53:56 +00:00
|
|
|
|
/// Two functions: Removes expired channels, and announces expired votes.
|
2018-10-28 18:44:30 +00:00
|
|
|
|
/// </summary>
|
2018-10-29 02:53:56 +00:00
|
|
|
|
private async Task BackgroundCheckingTask()
|
|
|
|
|
{
|
|
|
|
|
while (!_backgroundWorkerCancel.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
try { await Task.Delay(12000, _backgroundWorkerCancel.Token); }
|
|
|
|
|
catch (TaskCanceledException) { return; }
|
|
|
|
|
|
|
|
|
|
foreach (var g in Client.Guilds)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-31 20:11:12 +00:00
|
|
|
|
var info = GetState<GuildInformation>(g.Id);
|
|
|
|
|
if (info == null) continue;
|
2018-10-29 02:53:56 +00:00
|
|
|
|
|
2018-10-31 20:11:12 +00:00
|
|
|
|
await BackgroundTempChannelExpiryCheck(g, info);
|
|
|
|
|
await BackgroundVoteSessionExpiryCheck(g, info);
|
2018-10-29 02:53:56 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log("Unhandled exception in background task when processing a single guild.").Wait();
|
|
|
|
|
Log(ex.ToString()).Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:11:12 +00:00
|
|
|
|
private async Task BackgroundTempChannelExpiryCheck(SocketGuild g, GuildInformation info)
|
2018-10-29 02:53:56 +00:00
|
|
|
|
{
|
|
|
|
|
SocketGuildChannel ch = null;
|
2018-10-31 20:11:12 +00:00
|
|
|
|
lock (info)
|
2018-10-29 02:53:56 +00:00
|
|
|
|
{
|
2018-10-31 20:11:12 +00:00
|
|
|
|
ch = info.GetTemporaryChannel(g);
|
2018-10-29 02:53:56 +00:00
|
|
|
|
if (ch == null) return; // No temporary channel. Nothing to do.
|
2018-10-31 20:11:12 +00:00
|
|
|
|
if (!info.IsTempChannelExpired()) return;
|
2018-10-29 02:53:56 +00:00
|
|
|
|
|
|
|
|
|
// If we got this far, the channel's expiring. Start the voting cooldown.
|
2018-10-31 20:11:12 +00:00
|
|
|
|
info.Voting.StartCooldown();
|
2018-10-29 02:53:56 +00:00
|
|
|
|
}
|
|
|
|
|
await ch.DeleteAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:11:12 +00:00
|
|
|
|
private async Task BackgroundVoteSessionExpiryCheck(SocketGuild g, GuildInformation info)
|
2018-10-29 02:53:56 +00:00
|
|
|
|
{
|
|
|
|
|
bool act;
|
|
|
|
|
string nameTest;
|
2018-10-31 20:11:12 +00:00
|
|
|
|
lock (info) {
|
|
|
|
|
act = info.Voting.IsSessionExpired();
|
|
|
|
|
nameTest = info.Config.VoteChannel;
|
2018-11-26 02:02:40 +00:00
|
|
|
|
if (act) info.Voting.StartCooldown();
|
2018-10-29 02:53:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!act) return;
|
|
|
|
|
// Determine the voting channel; will send announcement there.
|
|
|
|
|
SocketTextChannel outCh = null;
|
|
|
|
|
foreach (var ch in g.TextChannels)
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(ch.Name, nameTest, StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
outCh = ch;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (outCh == null)
|
|
|
|
|
{
|
|
|
|
|
// Huh. Bad config?
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await outCh.SendMessageAsync(":x: Not enough votes were placed for channel creation.");
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2018-10-28 18:44:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|