public async Task ExecuteCommand(IDiscordInterface discordInterface, string[] args, SocketMessage rawMessage) { try { bool allowed = true; string reason = ""; // Check if we are allowed to do this command in this channel if (AllowedChannels.Length != 0 && !AllowedChannels.Contains(rawMessage.Channel.Name)) { allowed = false; reason += $"In channel {rawMessage.Channel.Name} but allowed channels are {String.Join(',', AllowedChannels)}\n"; } // Check if this user has a role that is in the whitelist if (AllowedRoles.Length != 0 && !rawMessage.Author.GetRolesAsString().ToList().Intersect(AllowedRoles).Any()) { allowed = false; reason += $"Allowed roles are {String.Join(',', AllowedRoles)} but user {rawMessage.Author.Username} " + $"only has roles {String.Join(',', rawMessage.Author.GetRolesAsString())}\n"; } // Check if this user has a role that is in the blacklist if (DisallowedRoles.Length != 0 && rawMessage.Author.GetRolesAsString().Intersect(DisallowedRoles).Any()) { allowed = false; reason += $"User {rawMessage.Author.Username} has blacklisted role/s " + $"{String.Join(',', rawMessage.Author.GetRolesAsString().Intersect(DisallowedRoles))}\n"; } if (allowed) { await Execute(discordInterface, args, rawMessage); } else { _logger.Warn($"User {rawMessage.Author.Username} tried to execute command {CommandString} but it failed because:\n{reason.Trim()}"); } } catch (Exception e) { _logger.Error(e, $"Unexpected error when attmpeting to exectue a command: {e.Message}"); } }
public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { var config = services.GetRequiredService <MajorContext>(); if (IncludeConfigValues) { var tempCollection = config.CommandChannels.Where(x => x.GuildId == context.Guild.Id).Select(x => x.ChannelId); AllowedChannels.AddRange(tempCollection); } var botOwner = services.GetRequiredService <IOptions <MajorConfig> >().Value.BotOwner; if (context.User.Id == botOwner || context.User.Id == context.Guild.OwnerId || AllowedChannels.Count == 0 || AllowedChannels.Any(x => x == context.Channel.Id)) { return(Task.FromResult(PreconditionResult.FromSuccess())); } else { return(Task.FromResult(PreconditionResult.FromError("Invalid Channel"))); } }
private static Task <bool> ChannelAllowed(ulong?channelId) { return(Task.Run(() => channelId != null && AllowedChannels.Contains(channelId.Value))); }