public override Task <PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IServiceProvider services) { if (context.Channel.IsNsfw) { return(Task.FromResult(PreconditionResult.FromSuccess())); } else { return(Task.FromResult(PreconditionResult.FromError("This command may only be invoked in an NSFW channel."))); } }
public override async Task <PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map) { double cooldown; DateTime lastUse; using (var db = new DbContext()) { var userRepo = new UserRepository(db); var user = await userRepo.FetchUserAsync(context.User.Id); switch (command.Name) { case "Rob": cooldown = Config.ROB_COOLDOWN; lastUse = DateTime.Parse(user.LastRob); break; case "Steal": cooldown = Config.STEAL_COOLDOWN; lastUse = DateTime.Parse(user.LastSteal); break; case "Jump": cooldown = Config.JUMP_COOLDOWN; lastUse = DateTime.Parse(user.LastJump); break; case "W***e": cooldown = Config.WHORE_COOLDOWN; lastUse = DateTime.Parse(user.LastWhore); break; case "Withdraw": cooldown = Config.WITHDRAW_COOLDOWN; lastUse = DateTime.Parse(user.LastWithdraw); break; default: cooldown = Config.DEFAULT_COOLDOWN; lastUse = DateTime.Now; break; } if (DateTime.Now.Subtract(lastUse).TotalMilliseconds > cooldown) { return(PreconditionResult.FromSuccess()); } else { await Logger.Cooldown(context as SocketCommandContext, command.Name, TimeSpan.FromMilliseconds(cooldown - DateTime.Now.Subtract(lastUse).TotalMilliseconds)); return(PreconditionResult.FromError("")); } } }
public async Task <PreconditionResult> CheckPreconditionsAsync(ICommandContext context, IServiceProvider services = null) { services = services ?? EmptyServiceProvider.Instance; async Task <PreconditionResult> CheckGroups(IEnumerable <PreconditionAttribute> preconditions, string type) { foreach (IGrouping <string, PreconditionAttribute> preconditionGroup in preconditions.GroupBy(p => p.Group, StringComparer.Ordinal)) { if (preconditionGroup.Key == null) { foreach (PreconditionAttribute precondition in preconditionGroup) { var result = await precondition.CheckPermissionsAsync(context, this, services).ConfigureAwait(false); if (!result.IsSuccess) { return(result); } } } else { var results = new List <PreconditionResult>(); foreach (PreconditionAttribute precondition in preconditionGroup) { results.Add(await precondition.CheckPermissionsAsync(context, this, services).ConfigureAwait(false)); } if (!results.Any(p => p.IsSuccess)) { return(PreconditionGroupResult.FromError($"{type} precondition group {preconditionGroup.Key} failed.", results)); } } } return(PreconditionGroupResult.FromSuccess()); } var moduleResult = await CheckGroups(Module.Preconditions, "Module"); if (!moduleResult.IsSuccess) { return(moduleResult); } var commandResult = await CheckGroups(Preconditions, "Command"); if (!commandResult.IsSuccess) { return(commandResult); } return(PreconditionResult.FromSuccess()); }
public override async Task <PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map) { using (var db = new DbContext()) { var gangRepo = new GangRepository(db); if (await gangRepo.InGangAsync(context.User.Id, context.Guild.Id)) { return(PreconditionResult.FromError("You may not use this command while being a member of a gang.")); } } return(PreconditionResult.FromSuccess()); }
/// <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 PreconditionResult.FromError(ErrorMessage ?? "Command can only be run by the owner of the bot."); return PreconditionResult.FromSuccess(); default: return PreconditionResult.FromError($"{nameof(RequireOwnerAttribute)} is not supported by this {nameof(TokenType)}."); } }
public async Task <PreconditionResult> CheckPreconditionsAsync(ICommandContext context, object arg, IServiceProvider services = null) { services = services ?? EmptyServiceProvider.Instance; foreach (var precondition in Preconditions) { var result = await precondition.CheckPermissionsAsync(context, this, arg, services).ConfigureAwait(false); if (!result.IsSuccess) { return(result); } } return(PreconditionResult.FromSuccess()); }
public override async Task <PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map) { using (var db = new DbContext()) { var gangRepo = new GangRepository(db); if (!await gangRepo.InGangAsync(context.User.Id, context.Guild.Id)) { return(PreconditionResult.FromError("You must be in a gang to use this command.")); } if ((await gangRepo.FetchGangAsync(context.User.Id, context.Guild.Id)).LeaderId != context.User.Id) { return(PreconditionResult.FromError("Only the leader of a gang may use this command.")); } } return(PreconditionResult.FromSuccess()); }
public override async Task <PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map) { TimeSpan cooldown; DateTime lastUse; var user = UserRepository.FetchUser(context as SocketCommandContext); switch (command.Name.ToLower()) { case "w***e": cooldown = Config.WHORE_COOLDOWN; lastUse = user.W***e; break; case "jump": cooldown = Config.JUMP_COOLDOWN; lastUse = user.Jump; break; case "steal": cooldown = Config.STEAL_COOLDOWN; lastUse = user.Steal; break; case "rob": cooldown = Config.ROB_COOLDOWN; lastUse = user.Rob; break; case "withdraw": cooldown = Config.WITHDRAW_COOLDOWN; lastUse = user.Withdraw; break; default: return(PreconditionResult.FromSuccess()); } if (DateTime.UtcNow.Subtract(lastUse).TotalMilliseconds > cooldown.TotalMilliseconds) { return(PreconditionResult.FromSuccess()); } else { await Logger.Cooldown(context as SocketCommandContext, command.Name, cooldown.Subtract(DateTime.UtcNow.Subtract(lastUse))); return(PreconditionResult.FromError("")); } }
public override async Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, ParameterInfo parameter, object value, IServiceProvider services) { // Hierarchy is only available under the socket variant of the user. if (!(context.User is SocketGuildUser guildUser)) { return(PreconditionResult.FromError("This command cannot be used outside of a guild")); } SocketGuildUser targetUser; switch (value) { case SocketGuildUser targetGuildUser: targetUser = targetGuildUser; break; case ulong userId: targetUser = await context.Guild.GetUserAsync(userId).ConfigureAwait(false) as SocketGuildUser; break; default: throw new ArgumentOutOfRangeException(); } if (targetUser == null) { return(PreconditionResult.FromError("Target user not found")); } if (guildUser.Hierarchy < targetUser.Hierarchy) { return(PreconditionResult.FromError("You cannot target anyone else whose roles are higher than yours")); } var currentUser = await context.Guild.GetCurrentUserAsync().ConfigureAwait(false) as SocketGuildUser; if (currentUser?.Hierarchy < targetUser.Hierarchy) { return(PreconditionResult.FromError("The bot's role is lower than the targeted user.")); } return(PreconditionResult.FromSuccess()); }
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(PreconditionResult.FromError("Command must be used in a guild channel")); } if (!guildUser.GuildPermissions.Has(GuildPermission.Value)) { return(PreconditionResult.FromError($"Bot requires guild permission {GuildPermission.Value}")); } } if (ChannelPermission.HasValue) { var guildChannel = context.Channel as IGuildChannel; ChannelPermissions perms; if (guildChannel != null) { perms = guildUser.GetPermissions(guildChannel); } else { perms = ChannelPermissions.All(guildChannel); } if (!perms.Has(ChannelPermission.Value)) { return(PreconditionResult.FromError($"Bot requires channel permission {ChannelPermission.Value}")); } } return(PreconditionResult.FromSuccess()); }
public async Task <PreconditionResult> CheckPreconditionsAsync(ICommandContext context, object arg, IDependencyMap map = null) { if (map == null) { map = DependencyMap.Empty; } foreach (var precondition in Preconditions) { var result = await precondition.CheckPermissions(context, this, arg, map).ConfigureAwait(false); if (!result.IsSuccess) { return(result); } } return(PreconditionResult.FromSuccess()); }
public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { //Makes sure it's in a server if (context.User is SocketGuildUser gUser) { // If this command was executed by a user with administrator permission, return a success if (gUser.HasAdmin()) { return(Task.FromResult(PreconditionResult.FromSuccess())); } else { return(Task.FromResult(PreconditionResult.FromError("You don't have the permissions to use this."))); } } else { return(Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command."))); } }
public override async Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, ParameterInfo parameter, object value, IServiceProvider services) { if (context.User is not IGuildUser guildUser) { return(PreconditionResult.FromError("This command cannot be used outside of a guild")); } var targetUser = value switch { UserRef userRef => userRef.GuildUser, IGuildUser targetGuildUser => targetGuildUser, ulong userId => await context.Guild.GetUserAsync(userId), _ => throw new ArgumentOutOfRangeException("Unknown Type used in parameter that requires hierarchy"), }; if (targetUser == null) { if (value is UserRef) { return(PreconditionResult.FromSuccess()); } else { return(PreconditionResult.FromError("Target user not found")); } } if (guildUser.GetHierarchy() <= targetUser.GetHierarchy()) { return(PreconditionResult.FromError("You cannot target anyone else whose roles are higher than yours")); } var currentUser = await context.Guild.GetCurrentUserAsync(); if (currentUser?.GetHierarchy() < targetUser.GetHierarchy()) { return(PreconditionResult.FromError("The bot's role is lower than the targeted user.")); } return(PreconditionResult.FromSuccess()); }
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(PreconditionResult.FromError("Command must be used in a guild channel"))); } if (!guildUser.GuildPermissions.Has(GuildPermission.Value)) { return(Task.FromResult(PreconditionResult.FromError($"User requires guild permission {GuildPermission.Value}"))); } } if (ChannelPermission.HasValue) { var guildChannel = context.Channel as IGuildChannel; ChannelPermissions perms; if (guildChannel != null) { perms = guildUser.GetPermissions(guildChannel); } else { perms = ChannelPermissions.All(guildChannel); } if (!perms.Has(ChannelPermission.Value)) { return(Task.FromResult(PreconditionResult.FromError($"User requires channel permission {ChannelPermission.Value}"))); } } return(Task.FromResult(PreconditionResult.FromSuccess())); }
public override async Task <PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map) { var guildUser = await context.Guild.GetCurrentUserAsync(); if (GuildPermission.HasValue) { if (guildUser == null) { return(PreconditionResult.FromError("Command must be used in a guild channel")); } if (!guildUser.GuildPermissions.Has(GuildPermission.Value)) { return(PreconditionResult.FromError($"Command requires guild permission {GuildPermission.Value}")); } } if (ChannelPermission.HasValue) { var guildChannel = context.Channel as IGuildChannel; ChannelPermissions perms; if (guildChannel != null) { perms = guildUser.GetPermissions(guildChannel); } else { perms = ChannelPermissions.All(guildChannel); } if (!perms.Has(ChannelPermission.Value)) { return(PreconditionResult.FromError($"Command requires channel permission {ChannelPermission.Value}")); } } return(PreconditionResult.FromSuccess()); }
public override Task <PreconditionResult> CheckPermissions(IUserMessage context, Command executingCommand, object moduleInstance) { var guildUser = context.Author as IGuildUser; if (GuildPermission.HasValue) { if (guildUser == null) { return(Task.FromResult(PreconditionResult.FromError("Command must be used in a guild channel"))); } if (!guildUser.GuildPermissions.Has(GuildPermission.Value)) { return(Task.FromResult(PreconditionResult.FromError($"Command requires guild permission {GuildPermission.Value}"))); } } if (ChannelPermission.HasValue) { var guildChannel = context.Channel as IGuildChannel; ChannelPermissions perms; if (guildChannel != null) { perms = guildUser.GetPermissions(guildChannel); } else { perms = ChannelPermissions.All(guildChannel); } if (!perms.Has(ChannelPermission.Value)) { return(Task.FromResult(PreconditionResult.FromError($"Command requires channel permission {ChannelPermission.Value}"))); } } return(Task.FromResult(PreconditionResult.FromSuccess())); }
/// <inheritdoc /> public override async System.Threading.Tasks.Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { switch (context.Client.TokenType) { case TokenType.Bot: RestApplication application = (RestApplication)await context.Client.GetApplicationInfoAsync().ConfigureAwait(false); if (!Global.IsDev((SocketUser)context.User)) { if (Global.clientCommands == true && context.User.Id == context.Client.CurrentUser.Id) { return(PreconditionResult.FromSuccess()); } return(PreconditionResult.FromError(ErrorMessage ?? "Command can only be run by a listed developer of the bot.")); } return(PreconditionResult.FromSuccess()); default: return(PreconditionResult.FromError($"{nameof(RequireDeveloperAttribute)} is not supported by this {nameof(TokenType)}.")); } }
/// <inheritdoc /> public override async Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { switch (context.Client.TokenType) { case TokenType.Bot: if ((context.Client as BaseSocketClient).BaseConfig.OwnerIds != null && (context.Client as BaseSocketClient).BaseConfig.OwnerIds.Contains(context.User.Id)) { return(PreconditionResult.FromSuccess()); } IApplication application = await context.Client.GetApplicationInfoAsync().ConfigureAwait(false); if (application.Team == null || application.Team.TeamMembers.Where(x => x.User.Id == context.User.Id).Count() == 0) { ; } return(PreconditionResult.FromError(ErrorMessage ?? "Command can only be run by the owner of the bot.")); return(PreconditionResult.FromSuccess()); default: return(PreconditionResult.FromError($"{nameof(RequireOwnerAttribute)} is not supported by this {nameof(TokenType)}.")); } }
public override async Task <PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map) { switch (context.Client.TokenType) { case TokenType.Bot: var application = await context.Client.GetApplicationInfoAsync(); if (context.User.Id != application.Owner.Id) { return(PreconditionResult.FromError("Command can only be run by the owner of the bot")); } return(PreconditionResult.FromSuccess()); case TokenType.User: if (context.User.Id != context.Client.CurrentUser.Id) { return(PreconditionResult.FromError("Command can only be run by the owner of the bot")); } return(PreconditionResult.FromSuccess()); default: return(PreconditionResult.FromError($"{nameof(RequireOwnerAttribute)} is not supported by this {nameof(TokenType)}.")); } }
/// <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())); }
public async Task <PreconditionResult> CheckPreconditions(IUserMessage context) { foreach (PreconditionAttribute precondition in Module.Preconditions) { var result = await precondition.CheckPermissions(context, this, Module.Instance).ConfigureAwait(false); if (!result.IsSuccess) { return(result); } } foreach (PreconditionAttribute precondition in Preconditions) { var result = await precondition.CheckPermissions(context, this, Module.Instance).ConfigureAwait(false); if (!result.IsSuccess) { return(result); } } return(PreconditionResult.FromSuccess()); }
/// <inheritdoc /> public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { if (context.Channel is ITextChannel text && text.IsNsfw) { return(Task.FromResult(PreconditionResult.FromSuccess())); }
public override async Task <PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map) { var user = context.User as IGuildUser; var dbUser = await UserRepository.FetchUserAsync(context as SocketCommandContext); var guild = await GuildRepository.FetchGuildAsync(context.Guild.Id); switch (attribute) { case Attributes.BotOwner: if (!DEABot.Credentials.OwnerIds.Any(x => x == context.User.Id)) { return(PreconditionResult.FromError("Only an owner of this bot may use this command.")); } break; case Attributes.ServerOwner: if (user.Guild.OwnerId != user.Id && !user.RoleIds.Any(x => guild.ModRoles.Any(y => y.RoleId == x && y.PermissionLevel >= 3))) { return(PreconditionResult.FromError("Only the owner of this server may use this command.")); } break; case Attributes.Admin: if (!(context.User as IGuildUser).GuildPermissions.Administrator && !user.RoleIds.Any(x => guild.ModRoles.Any(y => y.RoleId == x && y.PermissionLevel >= 2))) { return(PreconditionResult.FromError("The administrator permission is required to use this command.")); } break; case Attributes.Moderator: if (!user.RoleIds.Any(x => guild.ModRoles.Any(y => y.RoleId == x))) { return(PreconditionResult.FromError("Only a moderator may use this command.")); } break; case Attributes.Nsfw: if (!guild.Nsfw) { return(PreconditionResult.FromError($"This command may not be used while NSFW is disabled. An administrator may enable with the " + $"`{guild.Prefix}ChangeNSFWSettings` command.")); } var nsfwChannel = await context.Guild.GetChannelAsync((ulong)guild.NsfwId); if (nsfwChannel != null && context.Channel.Id != guild.NsfwId) { return(PreconditionResult.FromError($"You may only use this command in {(nsfwChannel as ITextChannel).Mention}.")); } var nsfwRole = context.Guild.GetRole((ulong)guild.NsfwRoleId); if (nsfwRole != null && (context.User as IGuildUser).RoleIds.All(x => x != guild.NsfwRoleId)) { return(PreconditionResult.FromError($"You do not have permission to use this command.\nRequired role: {nsfwRole.Mention}")); } break; case Attributes.InGang: if (!(await GangRepository.InGangAsync(context.User.Id, context.Guild.Id))) { return(PreconditionResult.FromError("You must be in a gang to use this command.")); } break; case Attributes.NoGang: if (await GangRepository.InGangAsync(context.User.Id, context.Guild.Id)) { return(PreconditionResult.FromError("You may not use this command while in a gang.")); } break; case Attributes.GangLeader: if (!(await GangRepository.InGangAsync(context.User.Id, context.Guild.Id))) { return(PreconditionResult.FromError("You must be in a gang to use this command.")); } break; case Attributes.Jump: if (dbUser.Cash < guild.JumpRequirement) { return(PreconditionResult.FromError($"You do not have the permission to use this command.\nRequired cash: {guild.JumpRequirement.ToString("C", Config.CI)}.")); } break; case Attributes.Steal: if (dbUser.Cash < guild.StealRequirement) { return(PreconditionResult.FromError($"You do not have the permission to use this command.\nRequired cash: {guild.StealRequirement.ToString("C", Config.CI)}.")); } break; case Attributes.Bully: if (dbUser.Cash < guild.BullyRequirement) { return(PreconditionResult.FromError($"You do not have the permission to use this command.\nRequired cash: {guild.BullyRequirement.ToString("C", Config.CI)}.")); } break; case Attributes.Rob: if (dbUser.Cash < guild.RobRequirement) { return(PreconditionResult.FromError($"You do not have the permission to use this command.\nRequired cash: {guild.RobRequirement.ToString("C", Config.CI)}.")); } break; case Attributes.FiftyX2: if (dbUser.Cash < guild.FiftyX2Requirement) { return(PreconditionResult.FromError($"You do not have the permission to use this command.\nRequired cash: {guild.FiftyX2Requirement.ToString("C", Config.CI)}.")); } break; default: throw new Exception($"ERROR: The {attribute} attribute does not exist!"); } return(PreconditionResult.FromSuccess()); }