Esempio n. 1
0
        /// <inheritdoc />
        public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
        {
            bool isValid = false;

            if ((Contexts & ContextType.Guild) != 0)
            {
                isValid = context.Channel is IGuildChannel;
            }
            if ((Contexts & ContextType.DM) != 0)
            {
                isValid = isValid || context.Channel is IDMChannel;
            }
            if ((Contexts & ContextType.Group) != 0)
            {
                isValid = isValid || context.Channel is IGroupChannel;
            }

            if (isValid)
            {
                return(Task.FromResult(PreconditionResult.FromSuccess()));
            }
            else
            {
                return(Task.FromResult(PreconditionAttributeResult.FromError($"Invalid context for command; accepted contexts: {Contexts}.", this)));
            }
        }
Esempio n. 2
0
        /// <inheritdoc />
        public override async Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
        {
            switch (context.Client.TokenType)
            {
            case TokenType.Bot:
                var application = await context.Client.GetApplicationInfoAsync().ConfigureAwait(false);

                if (context.User.Id != application.Owner.Id)
                {
                    return(PreconditionAttributeResult.FromError("Command can only be run by the owner of the bot.", this));
                }
                return(PreconditionResult.FromSuccess());

            default:
                return(PreconditionAttributeResult.FromError($"{nameof(RequiresOwnerAttribute)} is not supported by this {nameof(TokenType)}.", this));
            }
        }
Esempio n. 3
0
        /// <inheritdoc />
        public override async Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
        {
            IGuildUser guildUser = null;

            if (context.Guild != null)
            {
                guildUser = await context.Guild.GetCurrentUserAsync().ConfigureAwait(false);
            }

            if (GuildPermission.HasValue)
            {
                if (guildUser == null)
                {
                    return(PreconditionAttributeResult.FromError("Command must be used in a guild channel.", this));
                }
                if (!guildUser.GuildPermissions.Has(GuildPermission.Value))
                {
                    return(PreconditionAttributeResult.FromError($"Bot requires guild permission {GuildPermission.Value}.", this));
                }
            }

            if (ChannelPermission.HasValue)
            {
                ChannelPermissions perms;
                if (context.Channel is IGuildChannel guildChannel)
                {
                    perms = guildUser.GetPermissions(guildChannel);
                }
                else
                {
                    perms = ChannelPermissions.All(context.Channel);
                }

                if (!perms.Has(ChannelPermission.Value))
                {
                    return(PreconditionAttributeResult.FromError($"Bot requires channel permission {ChannelPermission.Value}.", this));
                }
            }

            return(PreconditionResult.FromSuccess());
        }
        /// <inheritdoc />
        public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
        {
            var guildUser = context.User as IGuildUser;

            if (GuildPermission.HasValue)
            {
                if (guildUser == null)
                {
                    return(Task.FromResult(PreconditionAttributeResult.FromError("Command must be used in a guild channel.", this)));
                }
                if (guildUser.GuildPermissions.Has(GuildPermission.Value))
                {
                    return(Task.FromResult(PreconditionPermissionResult.FromError($"User requires guild permission {GuildPermission.Value}.", GuildPermission.Value & ~((GuildPermission)guildUser.GuildPermissions.RawValue), 0, this)));
                }
            }

            if (ChannelPermission.HasValue)
            {
                ChannelPermissions perms;
                if (context.Channel is IGuildChannel guildChannel)
                {
                    perms = guildUser.GetPermissions(guildChannel);
                }
                else
                {
                    perms = ChannelPermissions.All(context.Channel);
                }

                if (perms.Has(ChannelPermission.Value))
                {
                    return(Task.FromResult(PreconditionPermissionResult.FromError($"User requires channel permission {ChannelPermission.Value}.", 0, ChannelPermission.Value & ~((ChannelPermission)perms.RawValue), this)));
                }
            }

            return(Task.FromResult(PreconditionResult.FromSuccess()));
        }