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 System.Threading.Tasks;
using Discord.WebSocket; using Discord.WebSocket;
using NadekoBot.Services.Database; using NadekoBot.Services.Database;
using System.Threading;
using NLog;
namespace NadekoBot.Modules.Gambling namespace NadekoBot.Modules.Gambling
{ {
@ -25,7 +27,6 @@ namespace NadekoBot.Modules.Gambling
SneakyGameStatus SneakyGameStatus
} }
//flower reaction event //flower reaction event
public static readonly ConcurrentHashSet<ulong> _flowerReactionAwardedUsers = new ConcurrentHashSet<ulong>();
public static readonly ConcurrentHashSet<ulong> _sneakyGameAwardedUsers = 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; var channel = (ITextChannel)Context.Channel;
try try
{ {
switch (e) switch (e)
{ {
case CurrencyEvent.FlowerReaction: case CurrencyEvent.FlowerReaction:
@ -121,38 +121,99 @@ namespace NadekoBot.Modules.Gambling
return Task.Delay(0); 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, "Add 🌸 reaction to this message to get 100" + NadekoBot.BotConfig.CurrencySign,
footer: "This event is active for up to 24 hours.") footer: "This event is active for up to 24 hours.")
.ConfigureAwait(false); .ConfigureAwait(false);
NadekoBot.Client.MessageDeleted += MessageDeletedEventHandler;
try { await msg.AddReactionAsync("🌸").ConfigureAwait(false); }
catch
{
try { await msg.AddReactionAsync("🌸").ConfigureAwait(false); } try { await msg.AddReactionAsync("🌸").ConfigureAwait(false); }
catch catch
{ {
try { await msg.AddReactionAsync("🌸").ConfigureAwait(false); } try { await msg.DeleteAsync().ConfigureAwait(false); }
catch 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); } try { await CurrencyHandler.AddCurrencyAsync(r.User.Value, "Flower Reaction Event", 100, false).ConfigureAwait(false); } catch { }
catch { return; }
} }
} }
using (msg.OnReaction(async (r) => catch { }
{ }))
try {
{ 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 { }
}))
{ {
await Task.Delay(TimeSpan.FromHours(24)).ConfigureAwait(false); await Task.Delay(TimeSpan.FromHours(24), cancelToken).ConfigureAwait(false);
try { await msg.DeleteAsync().ConfigureAwait(false); } catch { }
_flowerReactionAwardedUsers.Clear();
} }
catch (OperationCanceledException)
{
}
if (cancelToken.IsCancellationRequested)
return;
_log.Warn("Stopping flower reaction event because it expired.");
await End();
} }
} }
} }