public async Task HandleEventAsync(object sender, CommandExecutingEvent @event)
        {
            string id = @event.CommandContext?.CommandRegistration?.Id;

            if (id == null)
            {
                return;
            }

            var cooldownSpan = await m_Plugin.GetCooldownSpan(@event.Actor, id);

            if (cooldownSpan.HasValue)
            {
                var lastExecuted = await m_CooldownManager.LastExecuted(@event.Actor, id);

                if (lastExecuted.HasValue)
                {
                    var spanSinceLast = DateTime.Now - lastExecuted.Value;

                    if (spanSinceLast < cooldownSpan)
                    {
                        @event.CommandContext.Exception = new UserFriendlyException(m_Plugin.StringLocalizer["cooldown:command", new { TimeLeft = cooldownSpan - spanSinceLast }]);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void OnExecutingCommand(CommandExecutedEvent @event, ref Task task)
        {
            var original = task;

            // Rocket's attempt at executing commands is hooked rather than using
            // its native event as it allows us to run asynchronous code

            async Task CheckCooldown()
            {
                if (!(@event.CommandContext.Exception is CommandNotFoundException) || R.Commands == null)
                {
                    return;
                }

                const string rocketPrefix = "rocket:";

                var commandAlias = @event.CommandContext.CommandAlias;

                if (string.IsNullOrEmpty(commandAlias))
                {
                    return;
                }

                if (commandAlias.StartsWith(rocketPrefix))
                {
                    commandAlias = commandAlias.Replace(rocketPrefix, string.Empty);
                }

                if (@event.Actor is UnturnedUser user)
                {
                    var           steamPlayer  = user.Player.SteamPlayer;
                    IRocketPlayer rocketPlayer = UnturnedPlayer.FromSteamPlayer(steamPlayer);

                    IRocketCommand command = R.Commands.GetCommand(commandAlias.ToLower());

                    if (command != null && R.Permissions.HasPermission(rocketPlayer, command))
                    {
                        string commandId = string.Format(RocketCooldownsFormat, command.Name);

                        var cooldownSpan = await m_CooldownsPluginAccessor.Instance.GetCooldownSpan(@event.Actor, commandId);

                        if (cooldownSpan.HasValue)
                        {
                            var lastExecuted = await m_CooldownManager.LastExecuted(@event.Actor, commandId);

                            if (lastExecuted.HasValue)
                            {
                                var spanSinceLast = DateTime.Now - lastExecuted.Value;

                                if (spanSinceLast < cooldownSpan)
                                {
                                    @event.CommandContext.Exception = new UserFriendlyException(
                                        m_CooldownsPluginAccessor.Instance.StringLocalizer["cooldown:command",
                                                                                           new { TimeLeft = cooldownSpan - spanSinceLast }]);

                                    @event.ExceptionHandled = false;

                                    return;
                                }
                            }

                            await m_CooldownManager.RecordExecution(@event.Actor, commandId, DateTime.Now);
                        }
                    }
                }

                await original;
            }

            task = CheckCooldown();
        }