Exemplo n.º 1
0
            public async Task CmdCooldown(CommandOrCrInfo command, int secs)
            {
                var channel = (ITextChannel)ctx.Channel;

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

                    return;
                }

                var name = command.Name.ToLowerInvariant();

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

                    var toDelete = config.CommandCooldowns.FirstOrDefault(cc => cc.CommandName == name);
                    if (toDelete != null)
                    {
                        uow._context.Set <CommandCooldown>().Remove(toDelete);
                    }
                    localSet.RemoveWhere(cc => cc.CommandName == name);
                    if (secs != 0)
                    {
                        var cc = new CommandCooldown()
                        {
                            CommandName = name,
                            Seconds     = secs,
                        };
                        config.CommandCooldowns.Add(cc);
                        localSet.Add(cc);
                    }
                    await uow.SaveChangesAsync();
                }
                if (secs == 0)
                {
                    var activeCds = ActiveCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet <ActiveCooldown>());
                    activeCds.RemoveWhere(ac => ac.Command == name);
                    await ReplyConfirmLocalizedAsync("cmdcd_cleared",
                                                     Format.Bold(name)).ConfigureAwait(false);
                }
                else
                {
                    await ReplyConfirmLocalizedAsync("cmdcd_add",
                                                     Format.Bold(name),
                                                     Format.Bold(secs.ToString())).ConfigureAwait(false);
                }
            }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the cooldown until a certain user can be targeted again.
        /// </summary>
        private T GetUserCooldown <T>(string commandName, IGuildUser user, bool sameParameters = false, string parameters = "") where T : UserCooldown
        {
            if (sameParameters && !string.IsNullOrEmpty(parameters))
            {
                CommandCooldown <UserCooldownParameters> sameParamCool = _data.Cooldowns.SameParameterCommands.FirstOrDefault(c => c.CommandName == commandName);

                // If the command couldn't be found in same parameter cooldowns list then the command has no cooldown options
                if (sameParamCool == null)
                {
                    return(null);
                }

                UserCooldownParameters usrCool = sameParamCool.Users.FirstOrDefault(u => u.Id == user.Id && u.Parameters == parameters);

                // This user isn't listed yet, meaning the cooldown has either expired or the user hasn't ran the command yet
                if (usrCool == null)
                {
                    return(null);
                }

                return((T)((UserCooldown)usrCool));
            }
            else if (sameParameters && string.IsNullOrEmpty(parameters))
            {
                throw new ArgumentException("Parameters are empty, and checking for same parameter cooldowns.");
            }
            else
            {
                CommandCooldown <UserCooldown> cmdCool = _data.Cooldowns.Commands.FirstOrDefault(c => c.CommandName == commandName);

                // If the command couldn't be found, then search in the same parameter cooldowns list
                if (cmdCool == null)
                {
                    return(null);
                }

                UserCooldown usrCool = cmdCool.Users.FirstOrDefault(u => u.Id == user.Id);

                // This user isn't listed yet, meaning the cooldown has either expired or the user hasn't ran the command yet.
                if (usrCool == null)
                {
                    return(null);
                }

                return((T)usrCool);
            }
        }
Exemplo n.º 3
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 = _db.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);
                }
            }
Exemplo n.º 4
0
            public async Task CmdCooldown(IUserMessage imsg, Command command, int secs)
            {
                var channel = (ITextChannel)imsg.Channel;

                if (secs < 0 || secs > 3600)
                {
                    await channel.SendErrorAsync("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, set => set.Include(gc => gc.CommandCooldowns));
                    var localSet = commandCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet <CommandCooldown>());

                    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)
                {
                    var activeCds = activeCooldowns.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet <ActiveCooldown>());
                    activeCds.RemoveWhere(ac => ac.Command == command.Text.ToLowerInvariant());
                    await channel.SendConfirmAsync($"🚮 Command **{command}** has no coooldown now and all existing cooldowns have been cleared.")
                    .ConfigureAwait(false);
                }
                else
                {
                    await channel.SendConfirmAsync($"✅ Command **{command}** now has a **{secs} {"seconds".SnPl(secs)}** cooldown.")
                    .ConfigureAwait(false);
                }
            }
Exemplo n.º 5
0
        /// <summary>
        /// Returns the <see cref="TimeSpan"/> until the specified command has expired (for the specified user).
        /// </summary>
        private TimeSpan GetTimeUntilCooldownHasExpired(string commandName, IGuildUser user, bool sameParameters, string parameters = "")
        {
            if (sameParameters && !string.IsNullOrEmpty(parameters))
            {
                CommandCooldown <UserCooldownParameters> sameParamCool = _data.Cooldowns.SameParameterCommands.FirstOrDefault(c => c.CommandName == commandName);
                UserCooldownParameters usrCool = sameParamCool.Users.FirstOrDefault(u => u.Id == user.Id && u.Parameters == parameters);

                DateTime executedTime = usrCool.CommandExecutedTime.ToDateTime();
                DateTime currentTime  = DateTime.UtcNow;

                return(executedTime.AddSeconds(sameParamCool.CooldownTime) - currentTime);
            }
            else
            {
                CommandCooldown <UserCooldown> cmdCool = _data.Cooldowns.Commands.FirstOrDefault(c => c.CommandName == commandName);
                UserCooldown usrCool = cmdCool.Users.FirstOrDefault(u => u.Id == user.Id);

                DateTime executedTime = usrCool.CommandExecutedTime.ToDateTime();
                DateTime currentTime  = DateTime.UtcNow;

                return(executedTime.AddSeconds(cmdCool.CooldownTime) - currentTime);
            }
        }
Exemplo n.º 6
0
        public bool TryAdd(CommandCooldown Cooldown)
        {
            var timer = new Timer(Expire, Cooldown, Cooldown.Length, Cooldown.Length);

            return(_cooldowns.TryAdd(Cooldown, timer));
        }