public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { // Check if the user is administrator and if it needs to apply cooldown for him. if (!AdminsAreLimited && context.User is IGuildUser user && user.GuildPermissions.Administrator) { return(Task.FromResult(PreconditionResult.FromSuccess())); } var key = new CooldownInfo(context.User.Id, command.GetHashCode()); // Check if message with the same hash code is already in dictionary if (_cooldowns.TryGetValue(key, out DateTime endsAt)) { // Calculate the difference between current time and the time cooldown should end var difference = endsAt.Subtract(DateTime.UtcNow); // Display message if command is on cooldown if (difference.Ticks > 0) { return(Task.FromResult(PreconditionResult.FromError($":cool::arrow_down_small::bangbang: Możesz użyć tej komendy za: {difference.ToString(@"mm\:ss")}"))); } // Update cooldown time var time = DateTime.UtcNow.Add(CooldownLength); _cooldowns.TryUpdate(key, time, endsAt); } else { _cooldowns.TryAdd(key, DateTime.UtcNow.Add(CooldownLength)); } return(Task.FromResult(PreconditionResult.FromSuccess())); }
public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { var guildAccountService = services.GetRequiredService <GuildAccountService>(); var cooldown = guildAccountService.GetCooldown($"{command.Module.Group}-{command.Name}", context.Guild.Id); var sGuildAccount = guildAccountService.GetSettingsAccount(context.Guild.Id); var allowedUsersAndRoles = sGuildAccount.AllowedUsersAndRolesToBypassCooldowns; var ts = TimeSpan.FromSeconds(cooldown); if (sGuildAccount.AllowAdminsToBypassCooldowns && context.User is IGuildUser user && user.GuildPermissions.Administrator || allowedUsersAndRoles.ValidatePermissions(context)) { return(Task.FromResult(PreconditionResult.FromSuccess())); } var key = new CooldownInfo(context.User.Id, command.GetHashCode()); if (_cooldowns.TryGetValue(key, out DateTime endsAt)) { var difference = endsAt.Subtract(DateTime.UtcNow); if (difference.Seconds > 0) { return(Task.FromResult(PreconditionResult.FromError($"You can use this command in {difference.ToString(@"mm\:ss")}m"))); } var time = DateTime.UtcNow.Add(ts); _cooldowns.TryUpdate(key, time, endsAt); } else { _cooldowns.TryAdd(key, DateTime.UtcNow.Add(ts)); } return(Task.FromResult(PreconditionResult.FromSuccess())); }
public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { if (!AdminsAreLimited && context.User is IGuildUser user && user.GuildPermissions.Administrator) { return(Task.FromResult(PreconditionResult.FromSuccess())); } var key = new CooldownInfo(context.User.Id, command.GetHashCode()); if (_cooldowns.TryGetValue(key, out DateTime endsAt)) { var difference = endsAt.Subtract(DateTime.UtcNow); if (difference.Ticks > 0) { return(Task.FromResult(PreconditionResult.FromError($"Możesz użyć tej komendy znów za: **{difference.ToString(@"mm\:ss")}**"))); } var time = DateTime.UtcNow.Add(CooldownLength); _cooldowns.TryUpdate(key, time, endsAt); } else { _cooldowns.TryAdd(key, DateTime.UtcNow.Add(CooldownLength)); } return(Task.FromResult(PreconditionResult.FromSuccess())); }
/// Checks if a user is on cooldown public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { CooldownInfo key = new CooldownInfo(context.User.Id, command.GetHashCode()); if (cooldowns.TryGetValue(key, out DateTime endsAt)) { TimeSpan difference = endsAt.Subtract(DateTime.Now); if (difference.Ticks > 0) { return(Task.FromResult( PreconditionResult.FromError( $"Please wait {difference:ss} seconds before trying again!"))); } DateTime time = DateTime.Now.Add(CooldownLength); cooldowns.TryUpdate(key, time, endsAt); } else { cooldowns.TryAdd(key, DateTime.Now.Add(CooldownLength)); } return(Task.FromResult(PreconditionResult.FromSuccess())); }
IEnumerator _StartCooldown(CooldownInfo cooldownInfo) { while (Time.time < (cooldownInfo.startTime + cooldownInfo.duration)) { float RemainingTime = (cooldownInfo.startTime + cooldownInfo.duration) - Time.time; float RemainingTimePct = RemainingTime / cooldownInfo.duration; // Update the cooldown image if (this.m_TargetGraphic != null) { this.m_TargetGraphic.fillAmount = RemainingTimePct; } // Update the text if (this.m_TargetText != null) { this.m_TargetText.text = RemainingTime.ToString("0"); } // Update the finish position this.UpdateFinishPosition(RemainingTimePct); yield return(0); } // Call the on finish this.OnCooldownCompleted(); }
public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { if (!AdminsAreLimited && context.User is IGuildUser user && user.GuildPermissions.ManageRoles) { return(Task.FromResult(PreconditionResult.FromSuccess())); } var key = new CooldownInfo(context.User.Id, command.GetHashCode()); if (_cooldowns.TryGetValue(key, out DateTime endsAt)) { var difference = endsAt.Subtract(DateTime.UtcNow); if (difference.Ticks > 0) { Task.Run(async() => await SendMessage(context, command)); return(Task.FromResult(PreconditionResult.FromError($"User: [{context.User.Username}] used {context.Message.Content} too fast! [{context.Guild.Name}] Channel: [{context.Channel.Name}]"))); } var time = DateTime.UtcNow.Add(CooldownLength); _cooldowns.TryUpdate(key, time, endsAt); } else { _cooldowns.TryAdd(key, DateTime.UtcNow.Add(CooldownLength)); } return(Task.FromResult(PreconditionResult.FromSuccess())); }
public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { var key = new CooldownInfo(context.User.Id, command.GetHashCode()); // Check if message with the same hash code is already in dictionary if (_cooldowns.TryGetValue(key, out var endsAt)) { // Calculate the difference between current time and the time cooldown should end var difference = endsAt.Subtract(DateTime.UtcNow); var timeSpanString = string.Format("{0:%s} seconds", difference); // Display message if command is on cooldown if (difference.Ticks > 0) { return(Task.FromResult( PreconditionResult.FromError($"You can use this command in {timeSpanString}"))); } // Update cooldown time var time = DateTime.UtcNow.Add(CooldownLength); _cooldowns.TryUpdate(key, time, endsAt); } else { _cooldowns.TryAdd(key, DateTime.UtcNow.Add(CooldownLength)); } return(Task.FromResult(PreconditionResult.FromSuccess())); }
/// <summary> /// Starts a cooldown. /// </summary> /// <param name="spellId">Spell identifier.</param> /// <param name="duration">Duration.</param> public void StartCooldown(int spellId, float duration) { if (!this.enabled || !this.gameObject.activeInHierarchy || this.m_TargetGraphic == null) { return; } // Save the spell id this.m_CurrentSpellId = spellId; // Enable the image if it's disabled if (!this.m_TargetGraphic.enabled) { this.m_TargetGraphic.enabled = true; } // Reset the fill amount this.m_TargetGraphic.fillAmount = 1f; // Enable the text if it's disabled if (this.m_TargetText != null) { if (!this.m_TargetText.enabled) { this.m_TargetText.enabled = true; } this.m_TargetText.text = duration.ToString("0"); } // Prepare the finish graphic if (this.m_FinishGraphic != null) { this.m_FinishGraphic.canvasRenderer.SetAlpha(0f); this.m_FinishGraphic.enabled = true; this.m_FinishGraphic.rectTransform.anchoredPosition = new Vector2( this.m_FinishGraphic.rectTransform.anchoredPosition.x, this.m_FinishOffsetY ); } // Set the slot on cooldown this.m_IsOnCooldown = true; // Create new cooldown info CooldownInfo cooldownInfo = new CooldownInfo(duration, Time.time, (Time.time + duration)); // Save that this spell is on cooldown if (!spellCooldowns.ContainsKey(spellId)) { spellCooldowns.Add(spellId, cooldownInfo); } // Start the coroutine this.StartCoroutine("_StartCooldown", cooldownInfo); }
/// <summary> /// Resumes a cooldown. /// </summary> /// <param name="spellId">Spell identifier.</param> public void ResumeCooldown(int spellId) { if (!this.enabled || !this.gameObject.activeInHierarchy || this.m_TargetGraphic == null) { return; } // Check if we have the cooldown info for that spell if (!spellCooldowns.ContainsKey(spellId)) { return; } // Get the cooldown info CooldownInfo cooldownInfo = spellCooldowns[spellId]; // Get the remaining time float remainingTime = (cooldownInfo.endTime - Time.time); float remainingTimePct = remainingTime / cooldownInfo.duration; // Save the spell id this.m_CurrentSpellId = spellId; // Enable the image if it's disabled if (!this.m_TargetGraphic.enabled) { this.m_TargetGraphic.enabled = true; } // Set the fill amount to the remaing percents this.m_TargetGraphic.fillAmount = (remainingTime / cooldownInfo.duration); // Enable the text if it's disabled if (this.m_TargetText != null) { if (!this.m_TargetText.enabled) { this.m_TargetText.enabled = true; } this.m_TargetText.text = remainingTime.ToString("0"); } // Update the finish if (this.m_FinishGraphic != null) { this.m_FinishGraphic.enabled = true; this.UpdateFinishPosition(remainingTimePct); } // Start the coroutine this.StartCoroutine("_StartCooldown", cooldownInfo); }
public void Add(T obj, float duration) { CooldownInfo info; if (_table.TryGetValue(obj, out info)) { info.Duration += duration; } else { _table[obj] = new CooldownInfo(obj, this.UpdateTimeSupplier.Total, duration); } }
/// <summary> /// Starts a cooldown. /// </summary> /// <param name="spellId">Spell identifier.</param> /// <param name="duration">Duration.</param> public void StartCooldown(int spellId, float duration) { if (!this.enabled || !this.gameObject.activeInHierarchy || this.m_TargetGraphic == null) { return; } // Save the spell id this.m_CurrentSpellId = spellId; // Enable the image if it's disabled if (!this.m_TargetGraphic.enabled) { this.m_TargetGraphic.enabled = true; } // Reset the fill amount this.m_TargetGraphic.fillAmount = 1f; // Enable the text if it's disabled if (this.m_TargetText != null) { if (!this.m_TargetText.enabled) { this.m_TargetText.enabled = true; } this.m_TargetText.text = duration.ToString("0"); } // Set the slot on cooldown this.m_IsOnCooldown = true; // Create new cooldown info CooldownInfo cooldownInfo = new CooldownInfo(duration, Time.time, (Time.time + duration)); // Save that this spell is on cooldown if (!spellCooldowns.ContainsKey(spellId)) { spellCooldowns.Add(spellId, cooldownInfo); } // Start the coroutine this.StartCoroutine("_StartCooldown", cooldownInfo); }
public AbilityUpdaterSystem(CooldownInfo abilityCooldownInfo, IApproximator <float> abilityCooldownApproximator) { if (abilityCooldownInfo == null) { throw new NullReferenceException($"{nameof(AbilityUpdaterSystem)} {nameof(abilityCooldownInfo)} was null"); } approximator = abilityCooldownApproximator; approximator.Set(new Dictionary <ushort, float> { { 0, float.PositiveInfinity } }, Time.time - Time.deltaTime); approximator.Set(new Dictionary <ushort, float> { { 0, float.PositiveInfinity } }, Time.time); _cooldownInfo = abilityCooldownInfo; }
private async Task SendMessage(ICommandContext context, CommandInfo command) { await context.Message.DeleteAsync(); var key = new CooldownInfo(context.User.Id, command.GetHashCode()); _cooldowns.TryGetValue(key, out DateTime endsAt); var difference = endsAt.Subtract(DateTime.UtcNow); const int delay = 3000; var embed = new EmbedBuilder(); embed.WithDescription($"{context.User.Mention} entspann dich. Versuch es in {Convert.ToInt32(difference.TotalSeconds)} Sekunde(n) noch einmal!"); embed.WithColor(new Color(90, 92, 96)); IUserMessage m = await context.Channel.SendMessageAsync("", false, embed.Build()); await Task.Delay(delay); await m.DeleteAsync(); }
IEnumerator _StartCooldown(CooldownInfo cooldownInfo) { while (Time.time < (cooldownInfo.startTime + cooldownInfo.duration)) { float RemainingTime = (cooldownInfo.startTime + cooldownInfo.duration) - Time.time; float RemainingTimePct = RemainingTime / cooldownInfo.duration; // Update the cooldown image if (this.m_TargetGraphic != null) this.m_TargetGraphic.fillAmount = RemainingTimePct; // Update the text if (this.m_TargetText != null) this.m_TargetText.text = RemainingTime.ToString("0"); // Update the finish position this.UpdateFinishPosition(RemainingTimePct); yield return 0; } // Call the on finish this.OnCooldownCompleted(); }
/// <summary> /// Starts a cooldown. /// </summary> /// <param name="spellId">Spell identifier.</param> /// <param name="duration">Duration.</param> public void StartCooldown(int spellId, float duration) { if (!this.enabled || !this.gameObject.activeInHierarchy || this.m_TargetGraphic == null) return; // Save the spell id this.m_CurrentSpellId = spellId; // Enable the image if it's disabled if (!this.m_TargetGraphic.enabled) this.m_TargetGraphic.enabled = true; // Reset the fill amount this.m_TargetGraphic.fillAmount = 1f; // Enable the text if it's disabled if (this.m_TargetText != null) { if (!this.m_TargetText.enabled) this.m_TargetText.enabled = true; this.m_TargetText.text = duration.ToString("0"); } // Prepare the finish graphic if (this.m_FinishGraphic != null) { this.m_FinishGraphic.canvasRenderer.SetAlpha(0f); this.m_FinishGraphic.enabled = true; this.m_FinishGraphic.rectTransform.anchoredPosition = new Vector2( this.m_FinishGraphic.rectTransform.anchoredPosition.x, this.m_FinishOffsetY ); } // Set the slot on cooldown this.m_IsOnCooldown = true; // Create new cooldown info CooldownInfo cooldownInfo = new CooldownInfo(duration, Time.time, (Time.time + duration)); // Save that this spell is on cooldown if (!spellCooldowns.ContainsKey(spellId)) spellCooldowns.Add(spellId, cooldownInfo); // Start the coroutine this.StartCoroutine("_StartCooldown", cooldownInfo); }