flowerreaction event under the hood improvements
This commit is contained in:
parent
ba8e906c97
commit
04ea479017
@ -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,12 +121,63 @@ 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
|
||||
{
|
||||
var msg = await Context.Channel.SendConfirmAsync("Flower reaction event started!",
|
||||
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)
|
||||
{
|
||||
_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
|
||||
{
|
||||
@ -149,10 +200,20 @@ namespace NadekoBot.Modules.Gambling
|
||||
catch { }
|
||||
}))
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromHours(24)).ConfigureAwait(false);
|
||||
try { await msg.DeleteAsync().ConfigureAwait(false); } catch { }
|
||||
_flowerReactionAwardedUsers.Clear();
|
||||
try
|
||||
{
|
||||
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();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user