2016-10-07 22:23:41 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using NadekoBot.Attributes;
|
|
|
|
|
using NadekoBot.Extensions;
|
|
|
|
|
using NadekoBot.Services;
|
|
|
|
|
using NadekoBot.Services.Database;
|
|
|
|
|
using NadekoBot.Services.Database.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
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]
|
|
|
|
|
public class CmdCdsCommands
|
|
|
|
|
{
|
2016-10-10 04:12:22 +00:00
|
|
|
|
public static ConcurrentDictionary<ulong, ConcurrentHashSet<CommandCooldown>> commandCooldowns { get; }
|
|
|
|
|
private static ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>> activeCooldowns = new ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>>();
|
2016-10-07 22:23:41 +00:00
|
|
|
|
|
|
|
|
|
static CmdCdsCommands()
|
|
|
|
|
{
|
|
|
|
|
using (var uow = DbHandler.UnitOfWork())
|
|
|
|
|
{
|
|
|
|
|
var configs = uow.GuildConfigs.GetAll();
|
2016-10-10 04:38:20 +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)]
|
|
|
|
|
public async Task CmdCooldown(IUserMessage imsg, Command command, int secs)
|
|
|
|
|
{
|
|
|
|
|
var channel = (ITextChannel)imsg.Channel;
|
|
|
|
|
if (secs < 0 || secs > 3600)
|
|
|
|
|
{
|
|
|
|
|
await channel.SendMessageAsync("Invalid second parameter. (Must be a number between 0 and 3600)").ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var uow = DbHandler.UnitOfWork())
|
|
|
|
|
{
|
|
|
|
|
var config = uow.GuildConfigs.For(channel.Guild.Id);
|
2016-10-10 04:12:22 +00:00
|
|
|
|
var localSet = commandCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<CommandCooldown>());
|
2016-10-07 22:23:41 +00:00
|
|
|
|
|
|
|
|
|
config.CommandCooldowns.RemoveWhere(cc => cc.CommandName == command.Text.ToLowerInvariant());
|
|
|
|
|
localSet.RemoveWhere(cc => cc.CommandName == command.Text.ToLowerInvariant());
|
|
|
|
|
if (secs != 0)
|
|
|
|
|
{
|
|
|
|
|
var cc = new CommandCooldown()
|
|
|
|
|
{
|
|
|
|
|
CommandName = command.Text.ToLowerInvariant(),
|
|
|
|
|
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-10-07 22:23:41 +00:00
|
|
|
|
activeCds.RemoveWhere(ac => ac.Command == command.Text.ToLowerInvariant());
|
|
|
|
|
await channel.SendMessageAsync($"Command **{command}** has no coooldown now and all existing cooldowns have been cleared.").ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
await channel.SendMessageAsync($"Command **{command}** now has a **{secs} {(secs == 1 ? "second" : "seconds")}** cooldown.").ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
public async Task AllCmdCooldowns(IUserMessage imsg)
|
|
|
|
|
{
|
|
|
|
|
var channel = (ITextChannel)imsg.Channel;
|
2016-10-10 04:12:22 +00:00
|
|
|
|
var localSet = commandCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<CommandCooldown>());
|
2016-10-07 22:23:41 +00:00
|
|
|
|
|
|
|
|
|
if (!localSet.Any())
|
|
|
|
|
await channel.SendMessageAsync("`No command cooldowns set.`").ConfigureAwait(false);
|
|
|
|
|
else
|
|
|
|
|
await channel.SendTableAsync("", localSet.Select(c => c.CommandName + ": " + c.Seconds + " secs"), s => $"{s,-30}", 2).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool HasCooldown(Command cmd, IGuild guild, IUser user)
|
|
|
|
|
{
|
2016-10-09 01:22:54 +00:00
|
|
|
|
if (guild == null)
|
|
|
|
|
return false;
|
2016-10-10 04:12:22 +00:00
|
|
|
|
var cmdcds = CmdCdsCommands.commandCooldowns.GetOrAdd(guild.Id, new ConcurrentHashSet<CommandCooldown>());
|
2016-10-07 22:23:41 +00:00
|
|
|
|
CommandCooldown cdRule;
|
|
|
|
|
if ((cdRule = cmdcds.FirstOrDefault(cc => cc.CommandName == cmd.Text.ToLowerInvariant())) != null)
|
|
|
|
|
{
|
2016-10-10 04:12:22 +00:00
|
|
|
|
var activeCdsForGuild = activeCooldowns.GetOrAdd(guild.Id, new ConcurrentHashSet<ActiveCooldown>());
|
2016-10-07 22:23:41 +00:00
|
|
|
|
if (activeCdsForGuild.FirstOrDefault(ac => ac.UserId == user.Id && ac.Command == cmd.Text.ToLowerInvariant()) != null)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
activeCdsForGuild.Add(new ActiveCooldown()
|
|
|
|
|
{
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
Command = cmd.Text.ToLowerInvariant(),
|
|
|
|
|
});
|
|
|
|
|
var t = Task.Run(async () =>
|
|
|
|
|
{
|
2016-10-24 21:05:49 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(cdRule.Seconds * 1000);
|
|
|
|
|
activeCdsForGuild.RemoveWhere(ac => ac.Command == cmd.Text.ToLowerInvariant() && ac.UserId == user.Id);
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
2016-10-07 22:23:41 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|