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.Extensions;
|
|
|
|
|
using NadekoBot.Services;
|
|
|
|
|
using NadekoBot.Services.Database.Models;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.Attributes;
|
|
|
|
|
using NadekoBot.Common.Collections;
|
|
|
|
|
using NadekoBot.Modules.Permissions.Services;
|
2016-10-07 22:23:41 +00:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Permissions
|
|
|
|
|
{
|
|
|
|
|
public partial class Permissions
|
|
|
|
|
{
|
|
|
|
|
[Group]
|
2017-02-24 09:53:42 +00:00
|
|
|
|
public class CmdCdsCommands : NadekoSubmodule
|
2016-10-07 22:23:41 +00:00
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
private readonly CmdCdService _service;
|
|
|
|
|
|
|
|
|
|
private ConcurrentDictionary<ulong, ConcurrentHashSet<CommandCooldown>> CommandCooldowns
|
|
|
|
|
=> _service.CommandCooldowns;
|
|
|
|
|
private ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>> ActiveCooldowns
|
|
|
|
|
=> _service.ActiveCooldowns;
|
2016-10-07 22:23:41 +00:00
|
|
|
|
|
2017-05-29 04:13:22 +00:00
|
|
|
|
public CmdCdsCommands(CmdCdService service, DbService db)
|
2016-10-07 22:23:41 +00:00
|
|
|
|
{
|
2017-05-27 23:51:22 +00:00
|
|
|
|
_service = service;
|
|
|
|
|
_db = db;
|
2016-10-07 22:23:41 +00:00
|
|
|
|
}
|
2017-05-27 23:51:22 +00:00
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 23:51:22 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2016-10-07 22:23:41 +00:00
|
|
|
|
{
|
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)
|
|
|
|
|
{
|
2017-05-27 23:51: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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|