possible fix for flowerreaction event on public nadeko

This commit is contained in:
Master Kwoth 2017-08-14 05:19:37 +02:00
parent 70906ed5cb
commit 7a1895bf31
3 changed files with 78 additions and 22 deletions

View File

@ -11,6 +11,8 @@ using NadekoBot.Common;
using NadekoBot.Common.Attributes;
using NadekoBot.Common.Collections;
using NLog;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace NadekoBot.Modules.Gambling
{
@ -137,14 +139,14 @@ namespace NadekoBot.Modules.Gambling
desc, footer: footer)
.ConfigureAwait(false);
await new FlowerReactionEvent(_client, _cs).Start(msg, context, amount);
await new FlowerReactionEvent(_client, _cs, amount).Start(msg, context);
}
}
}
public abstract class CurrencyEvent
{
public abstract Task Start(IUserMessage msg, ICommandContext channel, int amount);
public abstract Task Start(IUserMessage msg, ICommandContext channel);
}
public class FlowerReactionEvent : CurrencyEvent
@ -160,14 +162,39 @@ namespace NadekoBot.Modules.Gambling
private CancellationTokenSource Source { get; }
private CancellationToken CancelToken { get; }
public FlowerReactionEvent(DiscordSocketClient client, CurrencyService cs)
private readonly ConcurrentQueue<ulong> _toGiveTo = new ConcurrentQueue<ulong>();
private readonly int _amount;
public FlowerReactionEvent(DiscordSocketClient client, CurrencyService cs, int amount)
{
_log = LogManager.GetCurrentClassLogger();
_client = client;
_cs = cs;
_botUser = client.CurrentUser;
_amount = amount;
Source = new CancellationTokenSource();
CancelToken = Source.Token;
var _ = Task.Run(async () =>
{
var users = new List<ulong>();
while (!CancelToken.IsCancellationRequested)
{
await Task.Delay(1000).ConfigureAwait(false);
while (_toGiveTo.TryDequeue(out var usrId))
{
users.Add(usrId);
}
if (users.Count > 0)
{
await _cs.AddToManyAsync("", _amount, users.ToArray()).ConfigureAwait(false);
}
users.Clear();
}
}, CancelToken);
}
private async Task End()
@ -191,7 +218,7 @@ namespace NadekoBot.Modules.Gambling
return Task.CompletedTask;
}
public override async Task Start(IUserMessage umsg, ICommandContext context, int amount)
public override async Task Start(IUserMessage umsg, ICommandContext context)
{
StartingMessage = umsg;
_client.MessageDeleted += MessageDeletedEventHandler;
@ -206,7 +233,7 @@ namespace NadekoBot.Modules.Gambling
catch { return; }
}
}
using (StartingMessage.OnReaction(_client, async (r) =>
using (StartingMessage.OnReaction(_client, (r) =>
{
try
{
@ -215,8 +242,7 @@ namespace NadekoBot.Modules.Gambling
if (r.Emote.Name == "🌸" && r.User.IsSpecified && ((DateTime.UtcNow - r.User.Value.CreatedAt).TotalDays > 5) && _flowerReactionAwardedUsers.Add(r.User.Value.Id))
{
await _cs.AddAsync(r.User.Value, "Flower Reaction Event", amount, false)
.ConfigureAwait(false);
_toGiveTo.Enqueue(r.UserId);
}
}
catch

View File

@ -4,6 +4,7 @@ using Discord;
using NadekoBot.Extensions;
using NadekoBot.Services.Database.Models;
using NadekoBot.Services.Database;
using NadekoBot.Services;
namespace NadekoBot.Services
{
@ -60,6 +61,26 @@ namespace NadekoBot.Services
return true;
}
public async Task AddToManyAsync(string reason, long amount, params ulong[] userIds)
{
using (var uow = _db.UnitOfWork)
{
foreach (var userId in userIds)
{
var transaction = new CurrencyTransaction()
{
UserId = userId,
Reason = reason,
Amount = amount,
};
uow.Currency.TryUpdateState(userId, amount);
uow.CurrencyTransactions.Add(transaction);
}
await uow.CompleteAsync();
}
}
public async Task AddAsync(IUser author, string reason, long amount, bool sendMessage)
{
await AddAsync(author.Id, reason, amount);

View File

@ -23,6 +23,8 @@ namespace NadekoBot.Services.Discord
}
private Task Discord_ReactionsCleared(Cacheable<IUserMessage, ulong> msg, ISocketMessageChannel channel)
{
Task.Run(() =>
{
try
{
@ -30,11 +32,14 @@ namespace NadekoBot.Services.Discord
OnReactionsCleared?.Invoke();
}
catch { }
});
return Task.CompletedTask;
}
private Task Discord_ReactionRemoved(Cacheable<IUserMessage, ulong> msg, ISocketMessageChannel channel, SocketReaction reaction)
{
Task.Run(() =>
{
try
{
@ -42,11 +47,14 @@ namespace NadekoBot.Services.Discord
OnReactionRemoved?.Invoke(reaction);
}
catch { }
});
return Task.CompletedTask;
}
private Task Discord_ReactionAdded(Cacheable<IUserMessage, ulong> msg, ISocketMessageChannel channel, SocketReaction reaction)
{
Task.Run(() =>
{
try
{
@ -54,6 +62,7 @@ namespace NadekoBot.Services.Discord
OnReactionAdded?.Invoke(reaction);
}
catch { }
});
return Task.CompletedTask;
}