示例#1
0
            public async Task AllCmdCooldowns()
            {
                var channel  = (ITextChannel)Context.Channel;
                var localSet = CommandCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet <CommandCooldown>());

                if (!localSet.Any())
                {
                    await ReplyConfirmLocalized("cmdcd_none").ConfigureAwait(false);
                }
                else
                {
                    await channel.SendTableAsync("", localSet.Select(c => c.CommandName + ": " + c.Seconds + GetText("sec")), s => $"{s,-30}", 2).ConfigureAwait(false);
                }
            }
示例#2
0
            public async Task CmdCooldown(CommandInfo command, int secs)
            {
                var channel = (ITextChannel)Context.Channel;

                if (secs < 0 || secs > 3600)
                {
                    await ReplyErrorLocalized("invalid_second_param_between", 0, 3600).ConfigureAwait(false);

                    return;
                }

                using (var uow = DbHandler.UnitOfWork())
                {
                    var config   = uow.GuildConfigs.For(channel.Guild.Id, set => set.Include(gc => gc.CommandCooldowns));
                    var localSet = CommandCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet <CommandCooldown>());

                    config.CommandCooldowns.RemoveWhere(cc => cc.CommandName == command.Aliases.First().ToLowerInvariant());
                    localSet.RemoveWhere(cc => cc.CommandName == command.Aliases.First().ToLowerInvariant());
                    if (secs != 0)
                    {
                        var cc = new CommandCooldown()
                        {
                            CommandName = command.Aliases.First().ToLowerInvariant(),
                            Seconds     = secs,
                        };
                        config.CommandCooldowns.Add(cc);
                        localSet.Add(cc);
                    }
                    await uow.CompleteAsync().ConfigureAwait(false);
                }
                if (secs == 0)
                {
                    var activeCds = activeCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet <ActiveCooldown>());
                    activeCds.RemoveWhere(ac => ac.Command == command.Aliases.First().ToLowerInvariant());
                    await ReplyConfirmLocalized("cmdcd_cleared",
                                                Format.Bold(command.Aliases.First())).ConfigureAwait(false);
                }
                else
                {
                    await ReplyConfirmLocalized("cmdcd_add",
                                                Format.Bold(command.Aliases.First()),
                                                Format.Bold(secs.ToString())).ConfigureAwait(false);
                }
            }
示例#3
0
        public Task <bool> TryBlock(IGuild guild, IUser user, string commandName)
        {
            if (guild is null)
            {
                return(Task.FromResult(false));
            }

            var             cmdcds = CommandCooldowns.GetOrAdd(guild.Id, new ConcurrentHashSet <CommandCooldown>());
            CommandCooldown cdRule;

            if ((cdRule = cmdcds.FirstOrDefault(cc => cc.CommandName == commandName)) != null)
            {
                var activeCdsForGuild = ActiveCooldowns.GetOrAdd(guild.Id, new ConcurrentHashSet <ActiveCooldown>());
                if (activeCdsForGuild.FirstOrDefault(ac => ac.UserId == user.Id && ac.Command == commandName) != null)
                {
                    return(Task.FromResult(true));
                }

                activeCdsForGuild.Add(new ActiveCooldown()
                {
                    UserId  = user.Id,
                    Command = commandName,
                });

                var _ = Task.Run(async() =>
                {
                    try
                    {
                        await Task.Delay(cdRule.Seconds * 1000).ConfigureAwait(false);
                        activeCdsForGuild.RemoveWhere(ac => ac.Command == commandName && ac.UserId == user.Id);
                    }
                    catch
                    {
                        // ignored
                    }
                });
            }

            return(Task.FromResult(false));
        }
示例#4
0
        public Task <bool> TryBlockLate(DiscordSocketClient client, IUserMessage msg, IGuild guild,
                                        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));
        }