/// <summary> /// Adds a user to the cooldown list. If he already exists, the user's executed time is just changed. /// </summary> private void AddUserToCooldown(string commandName, IGuildUser user, bool sameParameters = false, string parameters = "") { if (sameParameters && !string.IsNullOrEmpty(parameters)) { CommandCooldown <UserCooldownParameters> cmdCool = _data.Cooldowns.SameParameterCommands.FirstOrDefault(c => c.CommandName == commandName); UserCooldownParameters usrCool = GetUserCooldown <UserCooldownParameters>(commandName, user, true, parameters); if (usrCool == null) { cmdCool.Users.Add(new UserCooldownParameters { Id = user.Id, CommandExecutedTime = DateTime.UtcNow.ToTimestamp(), Parameters = parameters }); } else { usrCool.CommandExecutedTime = DateTime.UtcNow.ToTimestamp(); } } else { CommandCooldown <UserCooldown> cmdCool = _data.Cooldowns.Commands.FirstOrDefault(c => c.CommandName == commandName); UserCooldown usrCool = GetUserCooldown <UserCooldown>(commandName, user, false, ""); if (usrCool == null) { cmdCool.Users.Add(new UserCooldown { Id = user.Id, CommandExecutedTime = DateTime.UtcNow.ToTimestamp() }); } else { usrCool.CommandExecutedTime = DateTime.UtcNow.ToTimestamp(); } } _data.Cooldowns.Save("cooldowns.json"); }
/// <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); } }
/// <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); } }