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}"); } }
private static Task <bool> ChannelAllowed(ulong?channelId) { return(Task.Run(() => channelId != null && AllowedChannels.Contains(channelId.Value))); }