2017-07-17 19:42:36 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.Collections;
|
|
|
|
|
using NadekoBot.Common.ModuleBehaviors;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Permissions.Services
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public class CmdCdService : ILateBlocker, INService
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
|
|
|
|
public ConcurrentDictionary<ulong, ConcurrentHashSet<CommandCooldown>> CommandCooldowns { get; }
|
|
|
|
|
public ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>> ActiveCooldowns { get; } = new ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>>();
|
|
|
|
|
|
2017-10-13 00:21:39 +00:00
|
|
|
|
public CmdCdService(NadekoBot bot)
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
|
|
|
|
CommandCooldowns = new ConcurrentDictionary<ulong, ConcurrentHashSet<CommandCooldown>>(
|
2017-10-13 00:21:39 +00:00
|
|
|
|
bot.AllGuildConfigs.ToDictionary(k => k.GuildId,
|
2017-05-27 23:51:22 +00:00
|
|
|
|
v => new ConcurrentHashSet<CommandCooldown>(v.CommandCooldowns)));
|
|
|
|
|
}
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
public Task<bool> TryBlockLate(DiscordSocketClient client, IUserMessage msg, IGuild guild,
|
2017-05-29 04:13:22 +00:00
|
|
|
|
IMessageChannel channel, IUser user, string moduleName, string commandName)
|
|
|
|
|
{
|
|
|
|
|
if (guild == null)
|
|
|
|
|
return Task.FromResult(false);
|
|
|
|
|
var cmdcds = CommandCooldowns.GetOrAdd(guild.Id, new ConcurrentHashSet<CommandCooldown>());
|
|
|
|
|
CommandCooldown cdRule;
|
|
|
|
|
if ((cdRule = cmdcds.FirstOrDefault(cc => cc.CommandName == commandName.ToLowerInvariant())) != null)
|
|
|
|
|
{
|
|
|
|
|
var activeCdsForGuild = ActiveCooldowns.GetOrAdd(guild.Id, new ConcurrentHashSet<ActiveCooldown>());
|
|
|
|
|
if (activeCdsForGuild.FirstOrDefault(ac => ac.UserId == user.Id && ac.Command == commandName.ToLowerInvariant()) != null)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
activeCdsForGuild.Add(new ActiveCooldown()
|
|
|
|
|
{
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
Command = commandName.ToLowerInvariant(),
|
|
|
|
|
});
|
|
|
|
|
var _ = Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(cdRule.Seconds * 1000);
|
|
|
|
|
activeCdsForGuild.RemoveWhere(ac => ac.Command == commandName.ToLowerInvariant() && ac.UserId == user.Id);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return Task.FromResult(false);
|
|
|
|
|
}
|
2017-05-27 23:51:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ActiveCooldown
|
|
|
|
|
{
|
|
|
|
|
public string Command { get; set; }
|
|
|
|
|
public ulong UserId { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|