2016-10-07 22:23:41 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2016-12-03 13:14:37 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2016-10-07 22:23:41 +00:00
|
|
|
|
using NadekoBot.Attributes;
|
|
|
|
|
using NadekoBot.Extensions;
|
|
|
|
|
using NadekoBot.Services;
|
|
|
|
|
using NadekoBot.Services.Database.Models;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Permissions
|
|
|
|
|
{
|
|
|
|
|
public partial class Permissions
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class ActiveCooldown
|
|
|
|
|
{
|
|
|
|
|
public string Command { get; set; }
|
|
|
|
|
public ulong UserId { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Group]
|
2017-02-24 09:53:42 +00:00
|
|
|
|
public class CmdCdsCommands : NadekoSubmodule
|
2016-10-07 22:23:41 +00:00
|
|
|
|
{
|
2017-02-24 09:53:42 +00:00
|
|
|
|
public static ConcurrentDictionary<ulong, ConcurrentHashSet<CommandCooldown>> CommandCooldowns { get; }
|
2016-12-08 17:35:34 +00:00
|
|
|
|
private static ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>> activeCooldowns { get; } = new ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>>();
|
2016-10-07 22:23:41 +00:00
|
|
|
|
|
|
|
|
|
static CmdCdsCommands()
|
|
|
|
|
{
|
2016-12-30 04:46:13 +00:00
|
|
|
|
var configs = NadekoBot.AllGuildConfigs;
|
2017-02-24 09:53:42 +00:00
|
|
|
|
CommandCooldowns = new ConcurrentDictionary<ulong, ConcurrentHashSet<CommandCooldown>>(configs.ToDictionary(k => k.GuildId, v => new ConcurrentHashSet<CommandCooldown>(v.CommandCooldowns)));
|
2016-10-07 22:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-21 08:33:47 +00:00
|
|
|
|
public async Task CmdCooldown(CommandInfo command, int secs)
|
2016-10-07 22:23:41 +00:00
|
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
|
var channel = (ITextChannel)Context.Channel;
|
2016-10-07 22:23:41 +00:00
|
|
|
|
if (secs < 0 || secs > 3600)
|
|
|
|
|
{
|
2017-02-24 09:53:42 +00:00
|
|
|
|
await ReplyErrorLocalized("invalid_second_param_between", 0, 3600).ConfigureAwait(false);
|
2016-10-07 22:23:41 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var uow = DbHandler.UnitOfWork())
|
|
|
|
|
{
|
2016-12-03 13:14:37 +00:00
|
|
|
|
var config = uow.GuildConfigs.For(channel.Guild.Id, set => set.Include(gc => gc.CommandCooldowns));
|
2017-02-24 09:53:42 +00:00
|
|
|
|
var localSet = CommandCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<CommandCooldown>());
|
2016-10-07 22:23:41 +00:00
|
|
|
|
|
2016-12-16 18:43:57 +00:00
|
|
|
|
config.CommandCooldowns.RemoveWhere(cc => cc.CommandName == command.Aliases.First().ToLowerInvariant());
|
|
|
|
|
localSet.RemoveWhere(cc => cc.CommandName == command.Aliases.First().ToLowerInvariant());
|
2016-10-07 22:23:41 +00:00
|
|
|
|
if (secs != 0)
|
|
|
|
|
{
|
|
|
|
|
var cc = new CommandCooldown()
|
|
|
|
|
{
|
2016-12-16 19:45:46 +00:00
|
|
|
|
CommandName = command.Aliases.First().ToLowerInvariant(),
|
2016-10-07 22:23:41 +00:00
|
|
|
|
Seconds = secs,
|
|
|
|
|
};
|
|
|
|
|
config.CommandCooldowns.Add(cc);
|
|
|
|
|
localSet.Add(cc);
|
|
|
|
|
}
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
if (secs == 0)
|
|
|
|
|
{
|
2016-10-10 04:12:22 +00:00
|
|
|
|
var activeCds = activeCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<ActiveCooldown>());
|
2016-12-16 19:45:46 +00:00
|
|
|
|
activeCds.RemoveWhere(ac => ac.Command == command.Aliases.First().ToLowerInvariant());
|
2017-02-24 09:53:42 +00:00
|
|
|
|
await ReplyConfirmLocalized("cmdcd_cleared",
|
|
|
|
|
Format.Bold(command.Aliases.First())).ConfigureAwait(false);
|
2016-10-07 22:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2016-12-10 22:58:55 +00:00
|
|
|
|
{
|
2017-02-24 09:53:42 +00:00
|
|
|
|
await ReplyConfirmLocalized("cmdcd_add",
|
|
|
|
|
Format.Bold(command.Aliases.First()),
|
|
|
|
|
Format.Bold(secs.ToString())).ConfigureAwait(false);
|
2016-12-10 22:58:55 +00:00
|
|
|
|
}
|
2016-10-07 22:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-16 18:43:57 +00:00
|
|
|
|
public async Task AllCmdCooldowns()
|
2016-10-07 22:23:41 +00:00
|
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
|
var channel = (ITextChannel)Context.Channel;
|
2017-02-24 09:53:42 +00:00
|
|
|
|
var localSet = CommandCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<CommandCooldown>());
|
2016-10-07 22:23:41 +00:00
|
|
|
|
|
|
|
|
|
if (!localSet.Any())
|
2017-02-24 09:53:42 +00:00
|
|
|
|
await ReplyConfirmLocalized("cmdcd_none").ConfigureAwait(false);
|
2016-10-07 22:23:41 +00:00
|
|
|
|
else
|
2017-02-24 09:53:42 +00:00
|
|
|
|
await channel.SendTableAsync("", localSet.Select(c => c.CommandName + ": " + c.Seconds + GetText("sec")), s => $"{s,-30}", 2).ConfigureAwait(false);
|
2016-10-07 22:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-16 19:45:46 +00:00
|
|
|
|
public static bool HasCooldown(CommandInfo cmd, IGuild guild, IUser user)
|
2016-10-07 22:23:41 +00:00
|
|
|
|
{
|
2016-10-09 01:22:54 +00:00
|
|
|
|
if (guild == null)
|
|
|
|
|
return false;
|
2017-02-24 09:53:42 +00:00
|
|
|
|
var cmdcds = CmdCdsCommands.CommandCooldowns.GetOrAdd(guild.Id, new ConcurrentHashSet<CommandCooldown>());
|
2016-10-07 22:23:41 +00:00
|
|
|
|
CommandCooldown cdRule;
|
2016-12-16 19:45:46 +00:00
|
|
|
|
if ((cdRule = cmdcds.FirstOrDefault(cc => cc.CommandName == cmd.Aliases.First().ToLowerInvariant())) != null)
|
2016-10-07 22:23:41 +00:00
|
|
|
|
{
|
2016-10-10 04:12:22 +00:00
|
|
|
|
var activeCdsForGuild = activeCooldowns.GetOrAdd(guild.Id, new ConcurrentHashSet<ActiveCooldown>());
|
2016-12-16 19:45:46 +00:00
|
|
|
|
if (activeCdsForGuild.FirstOrDefault(ac => ac.UserId == user.Id && ac.Command == cmd.Aliases.First().ToLowerInvariant()) != null)
|
2016-10-07 22:23:41 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-02-14 13:30:21 +00:00
|
|
|
|
activeCdsForGuild.Add(new ActiveCooldown()
|
2016-10-07 22:23:41 +00:00
|
|
|
|
{
|
2017-02-14 13:30:21 +00:00
|
|
|
|
UserId = user.Id,
|
|
|
|
|
Command = cmd.Aliases.First().ToLowerInvariant(),
|
|
|
|
|
});
|
|
|
|
|
var _ = Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
2016-10-07 22:23:41 +00:00
|
|
|
|
{
|
2017-02-14 13:30:21 +00:00
|
|
|
|
await Task.Delay(cdRule.Seconds * 1000);
|
|
|
|
|
activeCdsForGuild.RemoveWhere(ac => ac.Command == cmd.Aliases.First().ToLowerInvariant() && ac.UserId == user.Id);
|
|
|
|
|
}
|
|
|
|
|
catch
|
2016-10-07 22:23:41 +00:00
|
|
|
|
{
|
2017-02-14 13:30:21 +00:00
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-10-07 22:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|