Exemplo n.º 1
0
        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()));
        }
Exemplo n.º 2
0
        /// 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()));
        }
Exemplo n.º 3
0
        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()));
        }
Exemplo n.º 4
0
        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()));
        }
Exemplo n.º 5
0
        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()));
        }
Exemplo n.º 6
0
        public override async Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
        {
            var user = context.User as SocketUser;

            if (user.Id == Config.Instance.OwnerId)
            {
                return(await Task.FromResult(PreconditionResult.FromSuccess()));
            }

            if (!AdminsAreLimited && ((IGuildUser)(user)).GuildPermissions.Administrator)
            {
                return(await Task.FromResult(PreconditionResult.FromSuccess()));
            }

            var key = new CooldownInfo(user.Id, command.GetHashCode());

            if (_cooldowns.TryGetValue(key, out var endsAt))
            {
                var difference = endsAt.Subtract(DateTime.UtcNow);
                if (difference.Ticks > 0)
                {
                    return(await Task.FromResult(PreconditionResult.FromError($"You can use this command in {difference.ToString(@"mm\:ss")}")));
                }

                var time = DateTime.UtcNow.Add(CooldownLength);
                _cooldowns.TryUpdate(key, time, endsAt);
            }
            else
            {
                _cooldowns.TryAdd(key, DateTime.UtcNow.Add(CooldownLength));
            }

            return(await Task.FromResult(PreconditionResult.FromSuccess()));
        }
Exemplo n.º 7
0
        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()));
        }
Exemplo n.º 8
0
        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();
        }