flowerreaction event under the hood improvements

This commit is contained in:
Kwoth 2017-02-06 12:57:38 +01:00
parent ba8e906c97
commit 04ea479017

View File

@ -11,6 +11,8 @@ using System.Text;
using System.Threading.Tasks;
using Discord.WebSocket;
using NadekoBot.Services.Database;
using System.Threading;
using NLog;
namespace NadekoBot.Modules.Gambling
{
@ -25,7 +27,6 @@ namespace NadekoBot.Modules.Gambling
SneakyGameStatus
}
//flower reaction event
public static readonly ConcurrentHashSet<ulong> _flowerReactionAwardedUsers = new ConcurrentHashSet<ulong>();
public static readonly ConcurrentHashSet<ulong> _sneakyGameAwardedUsers = new ConcurrentHashSet<ulong>();
@ -45,7 +46,6 @@ namespace NadekoBot.Modules.Gambling
var channel = (ITextChannel)Context.Channel;
try
{
switch (e)
{
case CurrencyEvent.FlowerReaction:
@ -121,38 +121,99 @@ namespace NadekoBot.Modules.Gambling
return Task.Delay(0);
}
public static async Task FlowerReactionEvent(CommandContext Context)
public static Task FlowerReactionEvent(CommandContext Context) =>
new FlowerReactionEvent().Start(Context);
}
}
public abstract class CurrencyEvent
{
public abstract Task Start(CommandContext channel);
}
public class FlowerReactionEvent : CurrencyEvent
{
public readonly ConcurrentHashSet<ulong> _flowerReactionAwardedUsers = new ConcurrentHashSet<ulong>();
private readonly Logger _log;
private IUserMessage msg { get; set; } = null;
private CancellationTokenSource source { get; }
private CancellationToken cancelToken { get; }
public FlowerReactionEvent()
{
_log = LogManager.GetCurrentClassLogger();
source = new CancellationTokenSource();
cancelToken = source.Token;
}
private async Task End()
{
if(msg != null)
await msg.DeleteAsync().ConfigureAwait(false);
if(!source.IsCancellationRequested)
source.Cancel();
NadekoBot.Client.MessageDeleted -= MessageDeletedEventHandler;
}
private Task MessageDeletedEventHandler(ulong id, Optional<SocketMessage> _) {
if (msg?.Id == id)
{
var msg = await Context.Channel.SendConfirmAsync("Flower reaction event started!",
_log.Warn("Stopping flower reaction event because message is deleted.");
Task.Run(() => End());
}
return Task.CompletedTask;
}
public override async Task Start(CommandContext context)
{
msg = await context.Channel.SendConfirmAsync("Flower reaction event started!",
"Add 🌸 reaction to this message to get 100" + NadekoBot.BotConfig.CurrencySign,
footer: "This event is active for up to 24 hours.")
.ConfigureAwait(false);
NadekoBot.Client.MessageDeleted += MessageDeletedEventHandler;
try { await msg.AddReactionAsync("🌸").ConfigureAwait(false); }
catch
{
try { await msg.AddReactionAsync("🌸").ConfigureAwait(false); }
catch
{
try { await msg.AddReactionAsync("🌸").ConfigureAwait(false); }
catch
try { await msg.DeleteAsync().ConfigureAwait(false); }
catch { return; }
}
}
using (msg.OnReaction(async (r) =>
{
try
{
if (r.Emoji.Name == "🌸" && r.User.IsSpecified && ((DateTime.UtcNow - r.User.Value.CreatedAt).TotalDays > 5) && _flowerReactionAwardedUsers.Add(r.User.Value.Id))
{
try { await msg.DeleteAsync().ConfigureAwait(false); }
catch { return; }
try { await CurrencyHandler.AddCurrencyAsync(r.User.Value, "Flower Reaction Event", 100, false).ConfigureAwait(false); } catch { }
}
}
using (msg.OnReaction(async (r) =>
{
try
{
if (r.Emoji.Name == "🌸" && r.User.IsSpecified && ((DateTime.UtcNow - r.User.Value.CreatedAt).TotalDays > 5) && _flowerReactionAwardedUsers.Add(r.User.Value.Id))
{
try { await CurrencyHandler.AddCurrencyAsync(r.User.Value, "Flower Reaction Event", 100, false).ConfigureAwait(false); } catch { }
}
}
catch { }
}))
catch { }
}))
{
try
{
await Task.Delay(TimeSpan.FromHours(24)).ConfigureAwait(false);
try { await msg.DeleteAsync().ConfigureAwait(false); } catch { }
_flowerReactionAwardedUsers.Clear();
await Task.Delay(TimeSpan.FromHours(24), cancelToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
if (cancelToken.IsCancellationRequested)
return;
_log.Warn("Stopping flower reaction event because it expired.");
await End();
}
}
}